gjson provides a convenient way to read arbitrary JSON in Go.
go get -u github.com/winterssy/gjson
import "github.com/winterssy/gjson"
- Parse to
gjson.Object
data, err := ioutil.ReadFile("./testdata/music.json")
if err != nil {
log.Fatal(err)
}
obj, err := gjson.Parse(data)
if err != nil {
panic(err)
}
fmt.Println(obj.GetNumber("code"))
fmt.Println(obj.GetString("data", "total"))
fmt.Println(obj.GetArray("data", "list").Index(0).ToObject().GetString("name"))
// Output:
// 200
// 1917
// 告白气球
- Bind to struct
const dummyData = `
{
"code": 200,
"data": {
"list": [
{
"artist": "周杰伦",
"album": "周杰伦的床边故事",
"name": "告白气球"
},
{
"artist": "周杰伦",
"album": "说好不哭 (with 五月天阿信)",
"name": "说好不哭 (with 五月天阿信)"
}
]
}
}
`
var s struct {
Code int `json:"code"`
Data struct {
List gjson.Array `json:"list"`
} `json:"data"`
}
err := gjson.UnmarshalFromString(dummyData, &s)
if err != nil {
panic(err)
}
fmt.Println(s.Data.List.Index(0).ToObject().GetString("name"))
// Output:
// 告白气球
Build with jsoniter
gjson
uses encoding/json
as default json package but you can change to jsoniter by build from other tags.
go build -tags=jsoniter .