dongzhent208577 2019-03-08 12:14
浏览 72
已采纳

动态数组json解析

I am trying to parse json to go lang struck but some how the object is return empty:

Json Object:

`{
    "names": [
        {
            "David": {
                "id": "100",
                "country": "usa",
                "group": [
                    "A1",
                    "A2"
                ]
            }
        },
        {
            "John": {
                "id": "1",
                "country": "uk",
                "group": [
                    "A1",
                    "A2"
                ]
            }
        }
    ]
}`

GoLang struct:

type Data struct {
    Names []Names `json:"names"`
}

type Names struct {
    ID      string   `json:"id"`
    Country string   `json:"country"`
    Group   []string `json:"group"`
}

The issue that array contains 2 element David,John somehow it return empty object as the parser has problem to extract the strings David,John

  • 写回答

2条回答 默认 最新

  • dongxingji3882 2019-03-08 12:20
    关注

    The struct Data's Names field is wrong. It is a slice of map[string]Names rather []Names. So just change it to:

    Names []map[string]Names `json:"names"`
    

    Check the full runnable code at https://play.golang.org/p/IDU0jANRbBn.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 CF1927D 求Hack