-2

i have problem to access fields by index. this library https://github.com/buger/jsonparser

example from https://github.com/buger/jsonparser:

// Or use can access fields by index!
jsonparser.GetInt("person", "avatars", "[0]", "url")

My code:

    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"))
    }

result in terminal: 0 Key path not found

raam86
  • 6,785
  • 2
  • 31
  • 46

1 Answers1

2

Person is not an array so you can not access it by index.

raam86
  • 6,785
  • 2
  • 31
  • 46
  • what i must do to access object by index? – Sergey Ryabushko Oct 07 '17 at 14:55
  • You can't access objects by index since they are not indexed, they are not sorted the key for the value you want to access is not a number so you can't call it by a number. think of it as a HashMap you can't access map values by numbers just the same way – raam86 Oct 11 '17 at 10:41