【发布时间】: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"}