-2

我无法按索引访问字段。这个库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 未找到密钥路径

4

1 回答 1

2

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

于 2017-10-06T13:47:41.637 回答