JSON在Go语言中嵌套了自身结构

时间:2017-01-06 09:45:10

标签: json go struct nested

我有以下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"`
}

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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 :(得分:0)

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

以下定义有效

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

答案 3 :(得分:0)

看起来肯定像你期望的那样定义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