如何获取所有Json Body的Children, Flutter
How to get all Json Body's Children, Flutter
请问,我的jsonbody有多个这样的Children吗
{ "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",
] }
] }
我的代码只能得到第一个Child。
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']
如果我想得到第二个 Child 我必须输入 ['body'][1]['children']
ok,但如果我想得到所有 Children 怎么办?
提前致谢
要获得 children 你需要地图函数。
jsonDecode(responseBody)['body'].map((item)=> item['children']).toList().expand((x) => x).toList()
请问,我的jsonbody有多个这样的Children吗
{ "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",
] }
] }
我的代码只能得到第一个Child。
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']
如果我想得到第二个 Child 我必须输入 ['body'][1]['children']
ok,但如果我想得到所有 Children 怎么办? 提前致谢
要获得 children 你需要地图函数。
jsonDecode(responseBody)['body'].map((item)=> item['children']).toList().expand((x) => x).toList()