dongle2627 2018-05-21 11:50
浏览 81
已采纳

作为对象的数组元素之一

The question is how to get the "from" element? The rest is not a problem

I know that in https://github.com/json-iterator/ it can be done, but I could not figure out how it works there

Json:

{
  "ab": 123456789,
  "cd": [
    [
      4,
      1234,
      123456,
      1000000001,
      1234567890,
      "text",
      {
        "from": "123456"
      }
    ],
    [
      4,
      4321,
      654321,
      1000000001,
      9876543210,
      "text",
      {
        "from": "654321"
      }
    ]
  ]
}

Golang:

type test struct {
    Ab int             `json:"ab"`
    Cd [][]interface{} `json:"cd"`
}
var testvar test
json.Unmarshal(Data, &testvar)
testvar.Cd[0][6]["from"].(string)

Error:

invalid operation: testvar.Cd[0][6]["from"] (type interface {} does not support indexing)
  • 写回答

1条回答 默认 最新

  • duanne9313 2018-05-21 12:06
    关注

    It's simple: it is a map[string]interface{} hence

    m, ok := testvar.Cd[0][6].(map[string]interface{})
    
    fmt.Println(m, ok, m["from"])
    

    https://play.golang.org/p/XJGs_HsxMfZ

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源