I'm writting a piece of Go to send json data on multicast udp:
func send(a string, messages chan interface{}) {
addr, err := net.ResolveUDPAddr("udp", a)
CheckError(err)
c, err := net.DialUDP("udp", nil, addr)
CheckError(err)
for {
msg := <-messages
myjson, err := json.Marshal(msg)
if err != nil {
fmt.Println("Error encoding JSON")
return
}
//Write to bytes to multicast UDP
c.Write([]byte(myjson))
time.Sleep(2 * time.Second)
}
}
So my json is converted to an array of byte to make it work. Here is my "receiver" func:
func serveMulticastUDP(a string, messages chan interface{}) {
addr, err := net.ResolveUDPAddr("udp", a)
CheckError(err)
l, err := net.ListenMulticastUDP("udp", nil, addr)
l.SetReadBuffer(maxDatagramSize)
for {
b := make([]byte, maxDatagramSize)
n, src, err := l.ReadFromUDP(b)
if err != nil {
log.Fatal("ReadFromUDP failed:", err)
}
s := string(b[:n])//here is my problem, I want s to be map[string]interface before sending in my channel
messages<-s
log.Println(s)
log.Println(src)
log.Println(n)
//h(src, n, b)
}
}
How can convert a array of bytes to map[string]interface (json) ?
In your code you used json.Marshal()
to convert your value to JSON text ([]byte
).
The other direction ([]byte
-> value) can be done using json.Unmarshal()
. json.Unmarshal()
expects a []byte
so you don't even have to convert it to string
.
See this example:
data := []byte(`{"key1":"value1","key2":123}`)
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
panic(err)
}
fmt.Printf("%+v", m)
Output (try it on the Go Playground):
map[key1:value1 key2:123]
Notes:
The result of marshaling (json.Marshal()
) is a value of type []byte
so you don't need explicit conversion here:
c.Write([]byte(myjson))
You can simply write:
c.Write(myjson)
Also when unmarshaling, make sure you pass b[:n]
to json.Unmarshal()
, as the rest of the slice contains 0
s (which your 2nd error suggests) but they are not part of the json text!