android - 如何从 Flutter 中获取 JSON 数据
问题描述
我一直在尝试从我的颤振应用程序中获取 json 数据。我设法在我的控制台上打印了整个 json。我只想从此 json 中获取“分数”值。我该怎么做?
final String url = "http://www.mocky.io/v2/5aa3f9ee310000c21926e2f8";
Future<String> getJsonData() async{
http.Response response = await http.get(
Uri.encodeFull(url),
headers: {"Accept" : "application/json"}
);
print(response.body);
List data = json.decode(response.body);
print(data[0]["score"]);
}
解决方案
将最后两行更新为
Map<String, dynamic> data = json.decode(response.body);
print(data["score"]);
说明:您从 API 收到的数据不是 JSON 数组,而是它的 JSON 对象,因此您应该解码对地图的响应,并从该地图中获取分数字段。
推荐阅读
- database - 如何在 Cassandra 中对数据进行建模以避免无用的重复
- arrays - 读取多个字符串的 VBScript 函数无法正常工作
- r - R-interp:相同的输入和输出坐标(x,y,xo,yo),多个数据值向量(z)。我可以跳过一些步骤吗?
- php - 在 Redhat 上使用 PDO 设置 SQL Server
- git - 如何从提交修正中恢复意外删除的未暂存更改?
- python - 使用docker运行tensorflow服务时如何选择一个指定的GPU
- python - 无法使用 openpyxl 在 Jupyter 中加载工作簿
- arrays - Swift - 获取自定义对象数组中所有数组的计数
- mysql - 存储过程无法识别参数。我的代码有什么问题?
- laravel - Vuetable-2 不适用于 Laravel Passport