复杂的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?

我需要将复杂的JSON对象编组为GO结构。 p>

所以我有来源: p>

 “ NetworkSettings”:{
“ Bridge”:“”,
“ SandboxID”:  “ e9736755bc41db307019fde3be0feed51086e6d3e23b0213c59bb5e43f7af214”,
“ HairpinMode”:false,
“ SecondaryIPAddresses”:null,
“ SecondaryIPv6Addresses”:null,
“ EndpointID” G“ e1b3e7e7b1e3e7e7b1e7e7b1e7e7b7e7a7b1e7e7b7e7a7b1e7a7e7a7b1e7b7e7a7b6e7e7b6e7e7b6b3b0b3b0e3b0e7b使用者 
“ IPAddress”:“ 172.17.0.2”,
“ IPPrefixLen”:16,
“ IPv6Gateway”:“”,
“ MacAddress”:“ 02:42:ac:11:00:02”,\  n“网络”:{
“网桥”:{
“ IPAMConfig”:空,
“链接”:空,
“别名”:空,
“ 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  “ 
} 
} 
  code>  pre> 
 
 

,我需要将其映射到这样的对象中: p>

  NetworkSettings  struct {
 IpAddress字符串
 SandboxID字符串
网关字符串
端口map [string] [] Binding 
网络map [string] map [string] string 
} 
  code>  pre> \  n 
 

但是由于 Networks map [string] map [string] string code>而出现错误 p>

  json:无法解组对象 进入字符串类型的Go值\恢复容器错误:json:无法将对象解组为字符串类型的Go值
  code>  pre> 
 
 

但这是我需要的复杂地图 地图结构。 p>

有什么想法吗? p> div>

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
}