【发布时间】:2020-10-15 22:34:27
【问题描述】:
请问,如果我的 json 身体有多个这样的孩子。
{ "head": { "title": "Podcasts",
"status": "200"}, "body": [
{ "element" : "outline",
"text" : "Music",
"children": [
{ "element" : "outline",
"type" : "link",
"text" : "Schlager",
"URL" : "http://opml.radiotime.com/Browse.ashx?id=c100000594&filter=p:topic",
"guide_id" : "c100000594" },
{ "element" : "outline",
"type" : "link",
"text" : "Rádios da Metropolitana FM",
"URL" : "http://opml.radiotime.com/Browse.ashx?id=c100002153&filter=p:topic",
"guide_id" : "c100002153" },
{ "element" : "outline",
"type" : "link",
"text" : "Hits Popular",
"URL" : "http://opml.radiotime.com/Browse.ashx?id=c100000599&filter=p:topic",
"guide_id" : "c100000599" },
{ "element" : "outline",
"type" : "link",
"text" : "Top 40 & Pop Music",
"URL" : "http://opml.radiotime.com/Browse.ashx?id=c57943&filter=p:topic",
"guide_id" : "c57943" },
] },
{ "element" : "outline",
"text" : "Talk",
"children": [
{ "element" : "outline",
"type" : "link",
"text" : "International Public Radio",
"URL" : "http://opml.radiotime.com/Browse.ashx?id=c100001487&filter=p:topic",
"guide_id" : "c100001487" },
{ "element" : "outline",
"type" : "link",
"text" : "History Podcasts",
"URL" : "http://opml.radiotime.com/Browse.ashx?id=c100000897&filter=p:topic",
"guide_id" : "c100000897" },
{ "element" : "outline",
] }
] }
我的代码只能得到第一个孩子。
List<Prodcasts> parseProdcasts(String responseBody) {
var parsed = jsonDecode(responseBody)['body'][0]['children'] as List;
return parsed.map<Prodcasts>((json) => Prodcasts.fromJson(json)).toList();
}
因为我输入了['body'][0]['children'],如果我想获得第二个孩子,我必须输入['body'][1]['children']
好的,但是如果我想得到所有的孩子怎么办? 提前致谢
【问题讨论】: