【问题标题】:JSON nested struct to itself in Go Language在 Go 语言中 JSON 嵌套结构到自身
【发布时间】:2017-01-06 09:45:10
【问题描述】:

我有以下 JSON

[{
  "id": 8,
  "title": "Indonesia",
  "type": "country",
  "attributes": {
    "regionCode": "ID",
    "information": {
      "title": "Welcome to Indonesia",
      "content": "We only serve selected areas in Indonesia.",
      "image": "indo.png"
    }
  },
  "children": [
    {
      "id": 9,
      "title": "Jakarta",
      "type": "city",
      "attributes": {
        "regionCode": "ID-JKT",
        "information": {
          "title": "Welcome to Capital City of Indonesia",
          "content": "We only serve selected areas in Jabotabek",
          "image": "jakarta.png"
        }
      }       
    },
    {
      "id": 10,
      "title": "Bali",
      "type": "city",
      "attributes": {
        "regionCode": "ID-BAL",
        "information": {
          "title": "Welcome to the beach city Bali",
          "content": "We only serve selected areas in Bali.",
          "image": "bali.png"
        }   
      }       
    }
  ]
}]

从技术上讲,这是一个作为子节点的嵌套结构,子节点也可能有子节点。但是当我使用 unmarshal 来解码它时,我只是无法正确处理它。

工作的如下i have no problem defining the attribute as a separate struct so assuming i have locationAttribute struct

type locationNode []struct {
    ID         string             `json:"id"`
    Title      string             `json:"title"`
    Type       string             `json:"type"`
    Attributes locationAttribute `json:"attributes"`
    Children   []struct {
        ID         string             `json:"id"`
        Title      string             `json:"title"`
        Type       string             `json:"type"`
        Attributes locationAttribute `json:"attributes"`
        Children   []interface{}      `json:"children"`
    } `json:"children"`
}

但我希望是下面的东西

type locationNode []struct {
    ID         string             `json:"id"`
    Title      string             `json:"title"`
    Type       string             `json:"type"`
    Attributes locationAttribute `json:"attributes"`
    Children   []locationNode `json:"children"`
}

任何帮助将不胜感激

【问题讨论】:

  • 问题是什么?您可以使用Children []locationNode,它会起作用。
  • 嗯,但我收到一个错误 {"error":"json: cannot unmarshal object into Go value of type []address.locationNode"}

标签: json go struct nested


【解决方案1】:

除了在 JSON 文本中 "id" 字段是一个数字而不是 string 的事实之外,它对我有用。

省略 "attributes" 以使示例更短:

type locationNode struct {
    ID       int            `json:"id"`
    Title    string         `json:"title"`
    Type     string         `json:"type"`
    Children []locationNode `json:"children"`
}

func main() {
    ln := locationNode{}

    err := json.Unmarshal([]byte(src), &ln)

    fmt.Printf("%+v %v\n", ln, err)
}

const src = `{
  "id": 8,
  "title": "Indonesia",
  "type": "country",
  "attributes": {
    "regionCode": "ID",
    "information": {
      "title": "Welcome to Indonesia",
      "content": "We only serve selected areas in Indonesia.",
      "image": "indo.png"
    }
  },
  "children": [
    {
      "id": 9,
      "title": "Jakarta",
      "type": "city",
      "attributes": {
        "regionCode": "ID-JKT",
        "information": {
          "title": "Welcome to Capital City of Indonesia",
          "content": "We only serve selected areas in Jabotabek",
          "image": "jakarta.png"
        }
      }       
    },
    {
      "id": 10,
      "title": "Bali",
      "type": "city",
      "attributes": {
        "regionCode": "ID-BAL",
        "information": {
          "title": "Welcome to the beach city Bali",
          "content": "We only serve selected areas in Bali.",
          "image": "bali.png"
        }   
      }       
    }
  ]
}`

输出(在Go Playground 上试试):

{ID:8 Title:Indonesia Type:country Children:[{ID:9 Title:Jakarta Type:city Children:[]} {ID:10 Title:Bali Type:city Children:[]}]} <nil>

【讨论】:

    【解决方案2】:

    使用 https://mholt.github.io/json-to-go/ 你的 JSON 转换为

    type AutoGenerated struct {
        ID int `json:"id"`
        Title string `json:"title"`
        Type string `json:"type"`
        Attributes struct {
            RegionCode string `json:"regionCode"`
            Information struct {
                Title string `json:"title"`
                Content string `json:"content"`
                Image string `json:"image"`
            } `json:"information"`
        } `json:"attributes"`
        Children []struct {
            ID int `json:"id"`
            Title string `json:"title"`
            Type string `json:"type"`
            Attributes struct {
                RegionCode string `json:"regionCode"`
                Information struct {
                    Title string `json:"title"`
                    Content string `json:"content"`
                    Image string `json:"image"`
                } `json:"information"`
            } `json:"attributes"`
        } `json:"children"`
    }
    

    【讨论】:

    • 感谢您的评论,但这并不理想,因为从技术上讲,孩子们可以生孩子等等。 “孩子”:[{“id”:9,“标题”:“雅加达”,“类型”:“城市”,“属性”:{},“孩子”:[{},{},{}]} ,...
    • 了解该工具的存在很重要,而且它应该让您在根据需要定制它时抢占先机。
    【解决方案3】:

    它看起来就像以您期望的方式定义Children 工作正常。以下编组和解组都很好。

    type Bob struct {
        ID         string            `json:"id"`
        Title      string            `json:"title"`
        Type       string            `json:"type"`
        Attributes int               `json:"attributes"`
        Children   []Bob             `json:"children"`
    }
    

    Playground link

    【讨论】:

      【解决方案4】:

      好的,我设法解决了这个问题 - 这实际上是一个愚蠢的粗心错误,因为我的结构已经是一个数组,我不应该将孩子定义为一个数组。

      所以下面的定义有效

      type locationNode []struct {
        ID         string             `json:"id"`
        Title      string             `json:"title"`
        Type       string             `json:"type"`
        Attributes locationAttribute  `json:"attributes"`
        Children   locationNode     `json:"children"`
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 2016-05-22
        • 1970-01-01
        • 1970-01-01
        • 2023-01-16
        相关资源
        最近更新 更多