Reputation: 37
I need to parse and get values from fields in a json file.
[{"id": 27}, {"id": 0, "label": "Label 0"}, null, {"id": 93}, {"id": 85}, {"id": 54}, null, {"id": 46, "label": "Label 46"}]}}
Though i can work on single level , i am at a loss how i can iterate through levels here. I have tried looking for an answer in google , various help sites and even stackoverflow. I could not find any example that might help me in working with multi level json byte array. Hope somebody can lead me to understand and work on it. Thanks in advance
Upvotes: 1
Views: 451
Reputation: 171
Just parse the JSON into an array of structs:
package main
import (
"encoding/json"
"fmt"
)
type Item struct {
Id int
Label string
}
func main() {
data := []byte(`[{"id": 27}, {"id": 0, "label": "Label 0"}, null, {"id": 93}, {"id": 85}, {"id": 54}, null, {"id": 46, "label": "Label 46"}]`)
var val []*Item
if err := json.Unmarshal(data, &val); err != nil {
fmt.Printf("Error: %s\n", val)
return
}
for _, it := range val {
fmt.Printf("%#v\n", it)
}
}
I hope this helps.
Upvotes: 4