【发布时间】:2021-09-08 07:50:47
【问题描述】:
这行不行,代码就放在那里。
var list = json.decode(response) as List<dynamic>;
我提交的响应字符串是:
final String response = await rootBundle.loadString('assets/madrid.json');
有了这个 JSON:
{
"coord": {
"lon": -3.7026,
"lat": 40.4165
},
"weather": [{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}],
"base": "stations",
"main": {
"temp": 297.84,
"feels_like": 297.62,
"temp_min": 294.82,
"temp_max": 299.94,
"pressure": 1017,
"humidity": 48
},
"visibility": 10000,
"wind": {
"speed": 1.34,
"deg": 270,
"gust": 2.68
},
"clouds": {
"all": 0
},
"dt": 1631051279,
"sys": {
"type": 2,
"id": 2007545,
"country": "ES",
"sunrise": 1630993686,
"sunset": 1631039875
},
"timezone": 7200,
"id": 3117735,
"name": "Madrid",
"cod": 200
}
该行不执行并卡在那里。我尝试了其他 JSON 响应,并且它有效,但我不知道为什么这个没有。
【问题讨论】:
-
卡住不工作是什么意思?有什么错误吗?
-
我尝试在dartpad.dev 对您的 json 数据进行 json.decode 并且效果很好
-
我有这个错误:期望一个“列表
”类型的值,但得到一个“_JsonMap”类型的值 -
好的,我刚试过:
print(json.decode(str));- 你的数据是Map,而不是List,所以你需要as Map<String, dynamic>; -
当我从列表更改为地图时出现以下错误:预期值为“Coord”类型,但得到一个“_JsonMap”类型的值