我正在尝试从JSON获取信息,但是出现问题。
这是JSON文件:
{
"version": "v1",
"resource": "***",
"action": "show",
"method": "GET",
"query": {
"type": "list",
"query": null,
"pagination": null
},
"request": [],
"data": {
"Total": [
{
"id": 34724,
"rank": 1,
"loss": 0,
}
....
],
"Away": [
{
"id": 3125"
"rank": 0"
}
....
]
}
}
这就是我尝试对其进行解码并存储数据的方式:
class Standings {
final String version;
final String resource;
final String action;
final String method;
List<Data> datas;
Standings(
{this.version, this.resource, this.action, this.method, this.datas});
factory Standings.fromJson(Map<String, dynamic> parsedJson) {
var listD = parsedJson['data'];
print(listD.runtimeType);
List<Data> dataList = listD.map((i) => Data.fromJson(i)).toList();
return Standings(
version: parsedJson['version'],
resource: parsedJson['resource'],
action: parsedJson['action'],
method: parsedJson['method'],
datas: dataList,
);
}
}
class Data {
List<Total> total;
List<Away> away;
Data(
{this.total, this.away});
factory Data.fromJson(Map<String, dynamic> parsedJson) {
var listT = parsedJson['Total'] as List;
print(listT.runtimeType);
List<Total> totalList = listT.map((i) => Total.fromJson(i)).toList();
var listA = parsedJson['Away'] as List;
print(listA.runtimeType);
List<Away> awayList = listA.map((i) => Away.fromJson(i)).toList();
return Data(
total: totalList,
away: awayList,
);
}
}
class Total {
final int id;
final int rank;
final int loss;
Total({
this.id,
this.rank,
this.loss,
});
factory Total.fromJson(Map<String, dynamic> parsedJson) {
return Total(
id: parsedJson['id'],
rank: parsedJson['rank'],
loss: parsedJson['loss'],
);
}
}
还是同一件事,但是我得到了这个错误:
数据在null上被调用
I / flutter(14264):W小工具库引起的异常
╞═════════════════════════════════════════════════ br
I / flutter(14264):在建筑物中引发了以下NoSuchMethodError
时间表(脏,状态:
I / flutter(14264):_ScheduleState#2dfb0(ticker未激活)):
I / flutter(14264):将getter'datas'调用为null。
I / flutter(14264):接收者:null
I / flutter(14264):尝试调用:数据
该怎么办?