【问题标题】:Trouble unmarshalling nested json with unknown keys使用未知键解组嵌套 json 时遇到问题
【发布时间】:2016-11-09 11:03:19
【问题描述】:

我在将以下格式的 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在生成的时候是未知的,会变化,有这个字段

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

就在没有外部密钥的服务器名称之后,这让我再次感到困惑。我尝试了很多组合来了解如何解组,但我失败了。

将 json 字段解组为结构的工作方法是什么?

【问题讨论】:

  • 使用map[string]whateverstruct 用各种键解组对象。
  • 谢谢@Volker,你拯救了我的一天。我相应地进行了修改并且它起作用了。您能否将其添加为答案,我会接受。

标签: json go unmarshalling


【解决方案1】:

您可以包含一个 map[string]ServerStruct 来满足您的要求。

您的结构可能如下所示:

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

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

使用一些额外的 json 标签,您将能够解析您的 json。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 对不起,昏迷无效,当我修剪 json 时。我会纠正它。但由于 json 具有在生成时未知的变量键,我无法将 server1.example.com 硬编码到结构中。这可能会改变。
    • 你是对的。当预先知道 JSON 格式时,该工具仍然是一个很好的工具。
    • 是的,同意。 +1 推荐这样一个方便的工具:-)
    猜你喜欢
    • 1970-01-01
    • 2014-08-31
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多