Trouble unmarshalling nested json with unknown keys

社会主义新天地 提交于 2019-12-12 01:22:21

问题


I am having trouble unmarshalling a json data of the below format to a struct. The structure of the json looks a bit confusing to me, so apologies for all the dumb things I am doing to unmarshal it.

{
  "message": {
    "Server1.example.com": [
      {
        "application": "Apache", 
        "host": {
          "name": "/^Server-[13456]/"
        }, 
        "owner": "User1", 
        "project": "Web", 
        "subowner": "User2"
      }
    ], 
    "Server2.example.com": [
      {
        "application": "Mysql", 
        "host": {
          "name": "/^Server[23456]/"
        }, 
        "owner": "User2", 
        "project": "DB", 
        "subowner": "User3"
      }
    ] 
  }, 
  "response_ms": 659, 
  "success": true
}

I am trying to unmarshal it using the following struct.

type ServerDetails struct  {
  Message  struct{
    Hostname struct{
      Details struct{
        Application string `json:"application"`
        }`json:"-"`
       }`json:"-"`
     }`json:"message"`
}

The fields Server[0-9].example.com will be unknown at the time of generating, and will change, and there is this field

      {
    "application": "Apache", 
    "host": {
      "name": "/^Server-[13456]/"
    },

just after the server name that doesn't have a key outside, which again looks confusing to me. I tried a good number of combinations to understand how this could be unmarshalled, but I failed.

What is a working approach to get the json fields unmarshal into a struct?


回答1:


You could include a map[string]ServerStruct to fullfill your requirements.

your struct could look like this:

type YourStruct struct {
    Success bool
    ResponseMS int
    Servers map[string]*ServerStruct
}

type ServerStruct struct {
    Application string
    Owner string
    [...]
}

With some additional json tags, you will be able to parse your json.




回答2:


You JSON is not valid with superfluous comma after the second ] Once you correct the JSON, you can use the excellent https://mholt.github.io/json-to-go/ to build the following Go struct

type AutoGenerated struct {
    Message struct {
        Server1ExampleCom []struct {
            Application string `json:"application"`
            Host struct {
                Name string `json:"name"`
            } `json:"host"`
            Owner string `json:"owner"`
            Project string `json:"project"`
            Subowner string `json:"subowner"`
        } `json:"Server1.example.com"`
        Server2ExampleCom []struct {
            Application string `json:"application"`
            Host struct {
                Name string `json:"name"`
            } `json:"host"`
            Owner string `json:"owner"`
            Project string `json:"project"`
            Subowner string `json:"subowner"`
        } `json:"Server2.example.com"`
    } `json:"message"`
    ResponseMs int `json:"response_ms"`
    Success bool `json:"success"`
}


来源:https://stackoverflow.com/questions/40505410/trouble-unmarshalling-nested-json-with-unknown-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!