tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.1k stars 846 forks source link

Result Array() function misleading comments #240

Closed hyde-zhang closed 2 years ago

hyde-zhang commented 2 years ago

The Result Array() function doesn't do what the comments says.

nonArrayData := "{\"jsonrpc\":\"2.0\",\"method\":\"subscription\",\"params\":{\"channel\":\"funny_channel\",\"data\":{\"name\":\"Jason\",\"company\":\"good_company\",\"number\":12345}}}"
parsed := gjson.Parse(nonArrayData)

for _, doc := range parsed.Get("params.data").Array() {
    log.Println(doc.Raw) // not called
}

arrayData := "{\"jsonrpc\":\"2.0\",\"method\":\"subscription\",\"params\":{\"channel\":\"funny_channel\",\"data\":[{\"name\":\"Jason\",\"company\":\"good_company\",\"number\":12345}]}}"
parsed = gjson.Parse(arrayData)

for _, doc := range parsed.Get("params.data").Array() {
    log.Println(doc.Raw)
}

Think the issue is this i.e. it skips too much. Either update the function commends or please don't skip '{'

tidwall commented 2 years ago

Thanks for reporting! I found the issue and fixed it in v1.9.4