gpt4 book ai didi

flutter - 如何在 flutter 中通过特定名称获取复杂的 json 数据

转载 作者:行者123 更新时间:2023-12-05 05:47:08 26 4
gpt4 key购买 nike

https://api.covid19api.com/summary这是我现在使用的 API 我可以通过下面的代码获取全局数据我想通过相同的方法获取单个国家(印度)的数据。如果没有方法可以获取数据,那么如果我使用“https://api.covid19api.com/total/dayone/country/India”,那么如何获取每日确诊病例。?

class GlobalSummaryModel{
final int newConfirmed;
final int totalConfirmed;
final int newDeaths;
final int totalDeaths;
final int newRecovered;
final int totalRecovered;
final DateTime date;

GlobalSummaryModel(this.newConfirmed, this.totalConfirmed, this.newDeaths, this.totalDeaths, this.newRecovered, this.totalRecovered, this.date);

factory GlobalSummaryModel.fromJson(Map<String, dynamic> json){
return GlobalSummaryModel(
json["Global"]["NewConfirmed"],
json["Global"]["TotalConfirmed"],
json["Global"]["NewDeaths"],
json["Global"]["TotalDeaths"],
json["Global"]["NewRecovered"],
json["Global"]["TotalRecovered"],
DateTime.parse(json["Date"]),
);
}
}

如果可以,请提供代码,这对我更有帮助,我是从 rest API 获取数据的新手。

最佳答案

API 还在响应中返回一个 Countries 字段,其中包含印度的数据。您可以像这样提取该数据:

final countries = json["Countries"];
final Map<String, dynamic> indiaSummaryData = countries.firstWhere((map) {
return map["CountryCode"] == "IN";
});

关于flutter - 如何在 flutter 中通过特定名称获取复杂的 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71055860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com