我肯定会向您推荐这个网站 json to dart应用程序快速输入 https://app.quicktype.io/只是不要忘记在右侧选择 Dart。
你只需将你的 json 放在 la left 中,就会给你这样的东西:
// To parse this JSON data, do
//
// final pickUp = pickUpFromJson(jsonString);
import 'dart:convert';
PickUp pickUpFromJson(String str) => PickUp.fromJson(json.decode(str));
String pickUpToJson(PickUp data) => json.encode(data.toJson());
class PickUp {
Results results;
PickUp({
this.results,
});
factory PickUp.fromJson(Map<String, dynamic> json) => new PickUp(
results: Results.fromJson(json["results"]),
);
Map<String, dynamic> toJson() => {
"results": results.toJson(),
};
}
class Results {
String totalRecordCount;
List<Record> records;
String result;
Results({
this.totalRecordCount,
this.records,
this.result,
});
factory Results.fromJson(Map<String, dynamic> json) => new Results(
totalRecordCount: json["TotalRecordCount"],
records: new List<Record>.from(json["Records"].map((x) => Record.fromJson(x))),
result: json["Result"],
);
Map<String, dynamic> toJson() => {
"TotalRecordCount": totalRecordCount,
"Records": new List<dynamic>.from(records.map((x) => x.toJson())),
"Result": result,
};
}
class Record {
String code;
String address;
String contactPhone;
Record({
this.code,
this.address,
this.contactPhone,
});
factory Record.fromJson(Map<String, dynamic> json) => new Record(
code: json["code"],
address: json["address"],
contactPhone: json["contact_phone"],
);
Map<String, dynamic> toJson() => {
"code": code,
"address": address,
"contact_phone": contactPhone,
};
}
一开始它会告诉您如何使用它。
// To parse this JSON data, do
//
// final pickUp = pickUpFromJson(jsonString);
所以当你在代码中调用它时,它会是这样的。
Future<Pickup> getPickup() async {
var response = await http.get(url);
return pickUpFromJson(response.body);
}
例如,可以为 FutureBuilder 调用此代码,或者在您将代码设置为等待未来的任何地方调用此代码。