https://play.golang.org/p/uvbJYQoqtl
On Mon, Jan 30, 2017 at 7:39 AM Konstantin Khomoutov < flatw...@users.sourceforge.net> wrote: > On Mon, 30 Jan 2017 07:14:23 -0800 (PST) > Rich <rma...@gmail.com> wrote: > > > If I have JSON that looks like this: > [...] > > > My question is that the JSON I have to parse the IPAddr is not > > > always the > > same. AND there are 50+ IPAddr= blocks... For example: > > > > > { > > > "value" : { > > > "IPAddr=10.1.1.12" : { > > > "ReplyTime" : { > [...] > > > } > > > }, > > > "IPAddr=10.1.1.145" : { > > "ReplyTime" : { > [...] > > } > > } (Keep adding blocks after this....) > > Your "value" field can be naturally represented as a map of keys > of the form "IPAddr=whatever" to structures of certain type > (IpAddress), so just do that [1]: > > ---------------->8---------------- > package main > > import ( > "encoding/json" > "fmt" > ) > > type IpAddress struct { > ReplyTime struct { > AverageTime int `json:"averageTime"` > MaxTime int `json:"maxTime"` > MinTime int `json:"minTime"` > } `json:"ReplyTime"` > } > > type Data struct { > Value map[string]IpAddress `json:"value"` > } > > const s = `{ > "value": { > "IPAddr=10.1.1.145" : { > "ReplyTime" : { > "minTime" : 42, > "maxTime" : 123, > "averageTime" : 12 > } > }, > "IPAddr=10.1.1.146" : { > "ReplyTime" : { > "minTime" : 111, > "maxTime" : 32, > "averageTime" : 15 > } > } > } > }` > > func main() { > var d Data > err := json.Unmarshal([]byte(s), &d) > if err != nil { > panic(err) > } > fmt.Printf("%v\n", d) > } > ---------------->8---------------- > > You can then iterate over the map to enumerate those key/value pairs. > > 1. https://play.golang.org/p/xmrTXb3SFL > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.