
void readData() {
databaseReference
.child("data")
.child("chapters")
.once()
.then((DataSnapshot snapshot) {
print('Data : ${snapshot.value}');
});,这是我的读数据函数,我得到了下面的JSON输出。
[
{
"name": "Global Village",
"videolists": [
{
"title": "Binary to decimal",
"link": "http://..........",
"duration": "5:3"
},
{
"title": "Binary to decimal",
"link": "http://..........",
"duration": "5:3"
}
]
},
{
"name": "Data Communication and Networking",
"videolists": [
{
"title": "Binary to decimal",
"link": "http://..........",
"duration": "5:3"
},
{
"title": "Binary to decimal",
"link": "http://..........",
"duration": "5:3"
}
]
}
]我试着用这个代码来解码JSON
var lists= json.decode(snapshot.value)为列表;
,但我遇到了一个像这样的错误
未处理异常:类型'List‘不是'String’的子类型
如何解码JSON?
发布于 2020-08-19 20:08:26
您可以尝试以下方法:
Iterable lists = json.decode(result);
List<dynamic> map = lists.toList();
map.forEach((res){
print(res["videolists"][0]);
});json.decode()接受一个String并返回一个dynamic,因此我们可以将它分配给类Iterable,然后我们可以使用toList()创建一个列表并使用forEach()进行迭代。以上所述应给予你:
{title: Binary to decimal, link: http://.........., duration: 5:3}
{title: Binary to decimal, link: http://.........., duration: 5:3}https://stackoverflow.com/questions/63493739
复制相似问题