dictionary - How to decode json in Go -
i have json this
[{"name":"name1","age":20},{"name":"name2","age":29}]
and want decode map this
map[map["name":"name1" ....] ,map["name":"name2",....]]
in case have logic this
bt:= []byte(metadatas[0]) var dat interface{} if err := json.unmarshal(bt, dat); err != nil { panic(err) } fmt.println(dat)
and response getting
[map["name":"name1" ....] ,map["name":"name2",....]]
how can map instead of
a target data structure unmarshaling can defined val := []map[string]interface{}{}
:
package main import ( "encoding/json" "fmt" ) func main() { input := []byte(`[{"name":"name1","age":20},{"name":"name2","age":29}]`) val := []map[string]interface{}{} if err := json.unmarshal(input, &val); err != nil { panic(err) } fmt.printf("%v\n", val) }
this gives slice of maps. if want put maps inside map, there should key elements of map (like indexes in slice).
Comments
Post a Comment