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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?