dssjxvbv918586 2017-01-06 09:45
浏览 115
已采纳

在Go语言中JSON嵌套结构本身

I have below 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"
        }   
      }       
    }
  ]
}]

Technically this is a nested structure to itself as the children node, and the children might have children as well. But i just couldnt get it right when im using unmarshal to decode this.

The working one is like below 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"`
}

However im expecting to be something like below

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

any help will be much appreciated

  • 写回答

4条回答 默认 最新

  • dongxing4805 2017-01-06 10:19
    关注

    Okay I managed to resolve this - it's actually a silly careless mistake, since my struct is already an array I should NOT define the children as an array.

    So below definition works

    type locationNode []struct {
      ID         string             `json:"id"`
      Title      string             `json:"title"`
      Type       string             `json:"type"`
      Attributes locationAttribute  `json:"attributes"`
      Children   locationNode     `json:"children"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址