遇到了解析未知键的嵌套 JSON 的问题。

huangapple go评论82阅读模式
英文:

Trouble unmarshalling nested json with unknown keys

问题

我遇到了一些问题,无法将下面格式的JSON数据解组成一个结构体。JSON的结构对我来说有点混乱,对于我做的所有愚蠢的解组尝试,我表示抱歉。

{
  "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
}

我尝试使用以下结构体进行解组:

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

在生成时,Server[0-9].example.com字段是未知的,并且会发生变化。而且,在服务器名称之后,有一个没有外部键的字段,这再次让我感到困惑。我尝试了很多组合来理解如何解组,但是失败了。

有什么有效的方法可以将JSON字段解组到结构体中吗?

英文:

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

得分: 3

你可以使用map[string]ServerStruct来满足你的需求。

你的结构体可以像这样定义:

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

type ServerStruct struct {
    Application string
    Owner       string
    // 其他字段...
}

通过添加一些额外的JSON标签,你就可以解析你的JSON数据了。

英文:

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

得分: 0

你的JSON在第二个"]"后面有多余的逗号,不符合规范。一旦你修正了JSON,你可以使用优秀的https://mholt.github.io/json-to-go/来构建以下Go结构体:

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"`
}
英文:

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"`
}

huangapple
  • 本文由 发表于 2016年11月9日 19:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/40505410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定