【发布时间】:2019-09-06 16:46:01
【问题描述】:
我无法从复杂的 json 中获取数据,下面是请求中的 json。
{
"results":{
"TotalRecordCount":"1",
"Records":[
{
"code":"PCK_34333338365C93E2D50DB9C",
"address":"1 AV KHEIREDDINE PACHA Imm Pacha centre BLOC B tunis Tunis 1000",
"contact_phone":"99608258"
}
],
"Result":"OK"
}
}
下面是我制作的模型。
import 'dart:convert';
class Pickup {
String status;
List message;
//Map<String ,dynamic> results;
Results results;
Pickup(
{this.status,
this.message,
this.results,
});
factory Pickup.fromJson(Map<String, dynamic> json) {
return Pickup(
status: json["status"] as String,
results: Results.fromJson(json["results"]),
);
}
}
class Results {
String TotalRecordCount;
records Records;
Results({this.TotalRecordCount,this.Records});
factory Results.fromJson(Map<String, dynamic> json) {
return Results(
TotalRecordCount: json["TotalRecordCount"],
Records:records.fromJson(json["Records"]),
);
}
}
class records{
String code;
String address;
String contact_phone;
records({
this.code,
this.address,
this.contact_phone
});
factory records.fromJson(Map<String, dynamic> json) {
return records(
code: json["code"],
address: json["address"],
contact_phone: json["contact_phone"],
);
}
}
现在我正在尝试解析记录以获取代码或地址并打印出来。
if (response.statusCode == 200) {
print(response.body);
final responseJson = json.decode(response.body);
var da=Pickup.fromJson(responseJson);
Results dat=da.results;
records data=dat.Records;
print(data.address);
}
response.body 工作正常,但是当我尝试解析结果或记录时,我得到 'List' is not a subtype of type 'Map
【问题讨论】:
-
使用在线Json to dart pojo转换器的最佳方式stackoverflow.com/questions/51901760/…
标签: arrays json list dart flutter