jsonparser 按索引访问字段

标签 json go jsonparser

我在按索引访问字段时遇到问题。这个图书馆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/

相关文章:

python - 在 pandas 数据框中存储复杂的字典

php - 向 PHP 发送动态数据

go - Golang中如何设置文本输入的初始值?

go - Go App 可以动态编译 Go 代码吗?

c - 使用 c 的 JSON 解析器

c# - 如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?

android - POST 请求 Android 带有 json 中的参数

Golang 嵌套 Yaml 值

json - 如何使用 bash 或 jq 在 linux 中解析 Json 文件以设置结果

java - 如何为具有以下输出的货币转换器 API 设计 JSON 解析器实体对象