我在按索引访问字段时遇到问题。这个库https://github.com/buger/jsonparser
https://github.com/buger/jsonparser中的示例:
// Or use can access fields by index!
jsonparser.GetInt("person", "avatars", "[0]", "url")
我的代码:
package main
import (
"github.com/buger/jsonparser"
"fmt"
)
func main () {
data := []byte(`{
"person": {
"name": {
"first": "Leonid",
"last": "Bugaev",
"fullName": "Leonid Bugaev"
},
"github": {
"handle": "buger",
"followers": 109
},
"avatars": [
{
"url": "https://avatars1.githubusercontent.com/u/14009?v=3&s=460",
"type": "thumbnail"
}
]
},
"company": {
"name": "Acme"
}
}`)
fmt.Println(jsonparser.GetInt(data, "person", "[2]", "[0]", "url"))
}
在终端中的结果:
0找不到密钥路径
最佳答案
Person不是数组,因此您无法通过索引访问它。
关于json - jsonparser按索引访问字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46607118/