go - how to access deeply nested json keys and values -


i'm writing websocket client in go. i'm receiving following json server:

{"args":[{"time":"2013-05-21 16:57:17"}],"name":"send:time"}

i'm trying access time parameter, can't grasp how reach deep interface type:

 package main;  import "encoding/json"  import "log"  func main() {     msg := `{"args":[{"time":"2013-05-21 16:56:16", "tzs":[{"name":"gmt"}]}],"name":"send:time"}`     u := map[string]interface{}{}     err := json.unmarshal([]byte(msg), &u)     if err != nil {         panic(err)     }     args := u["args"]     log.println( args[0]["time"] )   // invalid notation... } 

which errors, since notation not right:

   invalid operation: args[0] (index of type interface {}) 

i can't find way dig map grab nested keys , values.

once can on grabbing dynamic values, i'd declare these messages. how write type struct represent such complex data structs?

the interface{} part of map[string]interface{} decode match type of field. in case:

args.([]interface{})[0].(map[string]interface{})["time"].(string) 

should return "2013-05-21 16:56:16"

however, if know structure of json, should try defining struct matches structure , unmarshal that. ex:

type time struct {     time time.time      `json:"time"`     timezone []tzstruct `json:"tzs"` // obv. need define tzstruct     name string         `json:"name"` }  type timeresponse struct {     args []time         `json:"args"` }  var t timeresponse json.unmarshal(msg, &t) 

that may not perfect, should give idea


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -