【发布时间】:2020-11-27 08:34:58
【问题描述】:
我正在尝试将 JSON 数组转换为 Dart 列表。 因此,我首先使用 HTTP 包请求 GET 请求。
代码:
Future getCurrency() async {
final String url = 'https://xxx/$currency.php';
Map<String, String> headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Charset': 'utf-8'
};
Response response = await get(url, headers: headers);
data = json.decode(response.body);
print(data);
}
void initState() {
super.initState();
getCurrency();
}
然后我就卡在这里了。我真的不明白我怎么能做到这一点。 我的 JSON:
{
"sell": {
"value": "0",
"updated": "e",
"change": {
"percentage": "e%",
"sign": "increased"
}
},
"buy": {
"value": "e",
"updated": "e",
"change": {
"percentage": "%",
"sign": "increased"
}
}
}
【问题讨论】:
-
如果您能分享您的
data变量的内容,那就太好了,这样我们就可以帮助您将序列化为一个对象。 -
您无法将此数据解析为列表。您发送的 JSON 是一个对象,使用 jsonDecode 您会得到一个 Map
-
我不能将地图转换为列表吗?