gpt4 book ai didi

jsonparser 按索引访问字段

转载 作者:数据小太阳 更新时间:2023-10-29 03:39:56 25 4
gpt4 key购买 nike

我在按索引访问字段时遇到问题。这个图书馆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 不是数组,因此您不能通过索引访问它。

关于jsonparser 按索引访问字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46607118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com