如何将JSON转换为关联/索引数组

时间:2018-11-09 18:55:08

标签: json go

给出此JSON

{
  "users": [
    {
      "name" : "Elliot",
      "type" : "Reader",
      "age" : 23,
      "social" : {
        "facebook" : "https://facebook.com",
        "twitter" : "https://twitter.com"
      }
    },
    {
      "name" : "Fraser",
      "type" : "Author",
      "age" : 17,
      "social" : {
        "facebook" : "https://facebook.com",
        "twitter" : "https://twitter.com"
      }
    }
  ]
}

我需要一个函数/函数库来返回 map ,以便进行myMap[0].name来获取值“ Elliot”。 有谁可以帮助我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

  

我希望以非常漂亮的表示法关联数组(例如Res [“ users”] [0] .Name或类似名称)访问该字段。

正如评论中提到的,可以说 nicest 方式将使用结构

invisible()

结果:

name: Elliot, type: Reader, age: 23, facebook: https://facebook.com, twitter: https://twitter.com

Go Playground