dongxing7318 2016-07-07 10:57
浏览 45
已采纳

复杂的JSON封送处理到GoLang对象

I need to marshal complex JSON object to GO structure.

So I have source:

"NetworkSettings": {
        "Bridge": "",
        "SandboxID": "e9736755bc41db307019fde3be0feed51086e6d3e23b0213c59bb5e43f7af214",
        "HairpinMode": false,
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "2ee283787f45894c3383229d29ada1ccbb7f34b1c1e143e417e7ba75b7f5ebce",
        "Gateway": "172.17.0.1",
        "IPAddress": "172.17.0.2",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "MacAddress": "02:42:ac:11:00:02",
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "1c4788fac19973ddc73975613a2aef5a4cc2be446af458c06fa5fa162e8126ff",
                "EndpointID": "2ee283787f45894c3383229d29ada1ccbb7f34b1c1e143e417e7ba75b7f5ebce",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:02"
            }
        }

and I need to map it into object like this:

NetworkSettings struct {
    IpAddress string
    SandboxID string
    Gateway string
    Ports     map[string][]Binding
    Networks map[string]map[string]string
}

However because of Networks map[string]map[string]string I'm getting error

json: cannot unmarshal object into Go value of type string
error restoring containers: json: cannot unmarshal object into Go value of type string

But this is what I need to have a complex map of map structure.

Any idea how to do that?

  • 写回答

1条回答 默认 最新

  • dongtan1009 2016-07-07 11:08
    关注

    That error happens because some of the values in the JSON aren't strings, but are integers (IPPrefixLen for example), and therefore can't be assigned to a string variable.

    There are 2 ways to address this. You could use the interface{} type, so your type definition would become:

    NetworkSettings struct {
        IpAddress string
        SandboxID string
        Gateway   string
        Ports     map[string][]Binding
        Networks  map[string]map[string]interface{}
    }
    

    A better solution would probably be to define a type for the Network block:

    type Network struct {
        NetworkID   string 
        EndpointID  string
        Gateway     string
        IPAddress   string
        IPPrefixLen int
        //etc...
    }
    type NetworkSettings struct {
        IpAddress string
        SandboxID string
        Gateway   string
        Ports     map[string][]Binding
        Networks  map[string]Network
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献