【发布时间】:2025-11-26 21:45:01
【问题描述】:
我正在尝试解析这种类型的 json:
"teams": [
{
"id": 1,
"name": "New Jersey Devils",
"link": "/api/v1/teams/1",
"venue": {
"name": "Prudential Center",
"link": "/api/v1/venues/null",
"city": "Newark",
"timeZone": {
"id": "America/New_York",
"offset": -4,
"tz": "EDT"
}
},
"abbreviation": "NJD",
"teamName": "Devils",
"locationName": "New Jersey",
"firstYearOfPlay": "1982",
"division": {
"id": 18,
"name": "Metropolitan",
"nameShort": "Metro",
"link": "/api/v1/divisions/18",
"abbreviation": "M"
}.........
但我只想要 'id' 和 'name' 属性。
这是我的模型:
class TeamDetail{
final int id;
final String name;
TeamDetail({this.id, this.name});
factory TeamDetail.fromJson(Map<String, dynamic> json){
return TeamDetail(
id: json["id"],
name: json["name"],
);
}
}
还有我的方法:
Future<TeamDetail> getTeamDetail() async{
final response = await http.get('https://statsapi.web.nhl.com/api/v1/teams/1');
if(response.statusCode == 200){
return TeamDetail.fromJson(json.decode(response.body));
}else{
throw Exception("Failed to load");
}
}
但是当我调用该方法时,我总是收到一个空值。你能帮我吗。 谢谢
【问题讨论】: