jsonparser按索引访问字段

时间:2017-10-06 13:40:29

标签: 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未找到密钥路径

1 个答案:

答案 0 :(得分:2)

Person不是数组,因此您无法通过索引访问它。