2017-10-06 88 views
-2

我有問題通過索引訪問字段。這個庫https://github.com/buger/jsonparser按索引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鑰匙未找到路徑

回答

2

人不是數組,因此您無法通過索引訪問它。

+0

我必須通過索引訪問對象嗎? –

+0

您不能通過索引訪問對象,因爲它們沒有編入索引,它們沒有排序,您想訪問的值的密鑰不是數字,因此您無法通過編號調用它。把它看作是一個HashMap,你不能以同樣的方式通過數字訪問地圖值 – raam86