Re: [go-nuts] Parse JSON question

2017-01-30 Thread Rich
Thank you!! That worked brilliantly!

On Monday, January 30, 2017 at 10:39:56 AM UTC-5, Konstantin Khomoutov 
wrote:
>
> On Mon, 30 Jan 2017 07:14:23 -0800 (PST) 
> Rich  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), ) 
> 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.


Re: [go-nuts] Parse JSON question

2017-01-30 Thread Matt Harden
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  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), )
> 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.


Re: [go-nuts] Parse JSON question

2017-01-30 Thread Matt Harden
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  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), )
> 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.


[go-nuts] Parse JSON question

2017-01-30 Thread Rich
If I have JSON that looks like this:
>
> {
>
>"value" :  {
>
>   "IPAddr=10.1.1.12" : {
>
>  "ReplyTime" : {
>
> "minTime" : 0,
>
> "maxTime" : 0,
>
> "averageTime" : 0
>
>  }
>
>   }
>
> }
>
> }
>
> Normally you'd setup a struct: 

> type IpAddress struct {
>
> Value struct {
>
> IPAddr_10_1_1_12 struct {
>
> ReplyTime struct {
>
> AverageTime int `json:"averageTime"`
>
> MaxTime int `json:"maxTime"`
>
> MinTime int `json:"minTime"`
>
> } `json:"ReplyTime"`
>
> } `json:"IPAddr=10.1.1.12"`
>
> } `json:"value"`
>
> }
>
> 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" : {
>
> "minTime" : 0,
>
> "maxTime" : 0,
>
> "averageTime" : 0
>
>  }
>
>   },
>
>   "IPAddr=10.1.1.145" : {

 "ReplyTime" : {

"minTime" : 0,

"maxTime" : 0,

"averageTime" : 0

 }

  } (Keep adding blocks after this) 

} 

}


How would I setup a struct for something like this?
  

-- 
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.