My websocket server will receive and unmount JSON data. This data will always be wrapped in an object with key / value pairs. The key string will act as the value identifier, telling the Go server what value it has. Knowing what type of value I can go to JSON, unmarshal the value in the correct type of structure.
Each json object can contain several key / value pairs.
JSON example:
{ "sendMsg":{"user":"ANisus","msg":"Trying to send a message"}, "say":"Hello" }
Is there an easy way to use the "encoding/json" package for this?
package main import ( "encoding/json" "fmt" ) // the struct for the value of a "sendMsg"-command type sendMsg struct { user string msg string } // The type for the value of a "say"-command type say string func main(){ data := []byte(`{"sendMsg":{"user":"ANisus","msg":"Trying to send a message"},"say":"Hello"}`) // This won't work because json.MapObject([]byte) doesn't exist objmap, err := json.MapObject(data) // This is what I wish the objmap to contain //var objmap = map[string][]byte { // "sendMsg": []byte(`{"user":"ANisus","msg":"Trying to send a message"}`), // "say": []byte(`"hello"`), //} fmt.Printf("%v", objmap) }
Thanks for any suggestion / help!
json go map
ANisus Jun 16 2018-12-12T00: 00Z
source share