问题描述
我可以问一下,我的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","text" : "Rádios da Metropolitana FM","URL" : "http://opml.radiotime.com/browse.ashx?id=c100002153&filter=p:topic","guide_id" : "c100002153" },"text" : "Hits Popular","URL" : "http://opml.radiotime.com/browse.ashx?id=c100000599&filter=p:topic","guide_id" : "c100000599" },"text" : "Top 40 & Pop Music","URL" : "http://opml.radiotime.com/browse.ashx?id=c57943&filter=p:topic","guide_id" : "c57943" },] },"text" : "Talk","text" : "International Public Radio","URL" : "http://opml.radiotime.com/browse.ashx?id=c100001487&filter=p:topic","guide_id" : "c100001487" },"text" : "History podcasts","URL" : "http://opml.radiotime.com/browse.ashx?id=c100000897&filter=p:topic","guide_id" : "c100000897" },] }
] }
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']
好的,但是如果我想让所有的孩子们怎么办? 在此先感谢
解决方法
要生孩子,您需要地图功能。
jsonDecode(responseBody)['body'].map((item)=> item['children']).toList().expand((x) => x).toList()