-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
38 lines (34 loc) · 843 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"encoding/json"
"fmt"
jsoniterator "github.com/nilesh-akhade/json-iterator"
)
func main() {
iterator := jsoniterator.NewJsonRecordsIterator("fruits.json")
iterator.RegisterTypeDecoder("{.trees.[.{.fruits", func(d *json.Decoder, m map[string]interface{}) (interface{}, error) {
var fruit Fruit
err := d.Decode(&fruit)
if val, found := m["{.trees.[.{.id"]; found {
if strVal, ok := val.(string); ok {
fruit.TreeID = strVal
}
}
return fruit, err
})
for iterator.HasNext() {
item := iterator.Next()
if fruit, ok := item.(Fruit); ok {
fmt.Printf("%#v\n", fruit)
}
}
if iterator.Err() != nil {
fmt.Println(iterator.Err())
}
}
type Fruit struct {
TreeID string
Name string `json:"name,omitempty"`
Color string `json:"color,omitempty"`
Taste string `json:"taste,omitempty"`
}