Flutter 如何在flatter-dart上解析复杂的json
我从一个复杂的json中获取空数据,这里是json。当我试图解析它时,我得到了null。请看一看Flutter 如何在flatter-dart上解析复杂的json,flutter,dart,Flutter,Dart,我从一个复杂的json中获取空数据,这里是json。当我试图解析它时,我得到了null。请看一看 { "billInfoList": [ { "accountBalance": 0, "address": { "street": "nd", "complement": "df",
{
"billInfoList": [
{
"accountBalance": 0,
"address": {
"street": "nd",
"complement": "df",
"city": "dsfs",
"neighborhood": "cxcnx",
"isForcedAddress": false,
"state": "ARE",
"zip": "00000",
"country": "hchchc",
"district": "ccc"
},
"billDay": 0,
"billFrequency": 0,
"billNumber": "hcc",
"billType": 1cxc1,
"creationDate": "2019-02-04",
"dueDate": "2019-02-19",
"servicePeriodEnd": "2019-02-04",
"servicePeriodStart": "2019-01-29",
"status": "Paid",
"value": 0,
"valueDisputed": 0,
"valueOpen": 0
},
{
"accountBalance": 0,
"address": {
"street": "cxcvenciones ",
"complement": "Test124",
"city": "Arequipa",
"neighborhood": "Test4",
"isForcedAddress": false,
"state": "ARE",
"zip": "00320",
"country": "PE",
"district": "cquipa"
},
"billDay": 0,
"billFrequency": 0,
"billNumber": "dfs678",
"billType": fds1,
"billURL": "http://",
"creationDate": "2019-01-29",
"dueDate": "2019-02-13",
"servicePeriodEnd": "2019-01-29",
"servicePeriodStart": "2019-01-29",
"status": "Paid",
"value": 3.6,
"valueDisputed": 0,
"valueOpen": 0
},
{
"accountBalance": 0,
"address": {
"street": "fsdnciones ",
"complement": "Test124",
"city": "dfspa",
"neighborhood": "Test4",
"isForcedAddress": false,
"state": "ARE",
"zip": "3200",
"country": "PE",
"district": "requipa"
},
"billDay": 0,
"billFrequency": 0,
"billNumber": "323677",
"billType": 341,
"creationDate": "2019-01-29",
"dueDate": "2019-02-13",
"servicePeriodEnd": "2019-01-29",
"servicePeriodStart": "2019-01-29",
"status": "Pd",
"value": 0,
"valueDisputed": 0,
"valueOpen": 0
}
],
"TransactionSequenceId": "hrhrhrh9",
"ResponseCode": "hfhf00",
"ResponseMessage": "Request Processed successfully"
}
我已生成模型类:
生成模型类后,创建一个新的类名ModelClass
另一个类名APiResponse并编写代码:
此类获取api响应和返回响应:
fromJson(jsonDecode(response.body))
我已尝试解析:
APiResponse.fromJson(Map<String, dynamic> json)
: results = new ModelClass.fromJson(json),
error = "";
APiResponse.fromJson(映射json)
:results=newmodelclass.fromJson(json),
错误=”;
模型类:
class ModelClass {
List<BillInfoList> billInfoList;
String transactionSequenceId;
String responseCode;
String responseMessage;
ModelClass (
{this.billInfoList,
this.transactionSequenceId,
this.responseCode,
this.responseMessage});
ModelClass.fromJson(Map<String, dynamic> json) {
if (json['billInfoList'] != null) {
billInfoList = new List<BillInfoList>();
json['billInfoList'].forEach((v) {
billInfoList.add(new BillInfoList.fromJson(v));
});
}
transactionSequenceId = json['TransactionSequenceId'];
responseCode = json['ResponseCode'];
responseMessage = json['ResponseMessage'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.billInfoList != null) {
data['billInfoList'] = this.billInfoList.map((v) => v.toJson()).toList();
}
data['TransactionSequenceId'] = this.transactionSequenceId;
data['ResponseCode'] = this.responseCode;
data['ResponseMessage'] = this.responseMessage;
return data;
}
}
class BillInfoList {
int accountBalance;
Address address;
int billDay;
int billFrequency;
String billNumber;
int billType;
String creationDate;
String dueDate;
String servicePeriodEnd;
String servicePeriodStart;
String status;
double value;
int valueDisputed;
int valueOpen;
String billURL;
BillInfoList(
{this.accountBalance,
this.address,
this.billDay,
this.billFrequency,
this.billNumber,
this.billType,
this.creationDate,
this.dueDate,
this.servicePeriodEnd,
this.servicePeriodStart,
this.status,
this.value,
this.valueDisputed,
this.valueOpen,
this.billURL});
BillInfoList.fromJson(Map<String, dynamic> json) {
accountBalance = json['accountBalance'];
address =
json['address'] != null ? new Address.fromJson(json['address']) : null;
billDay = json['billDay'];
billFrequency = json['billFrequency'];
billNumber = json['billNumber'];
billType = json['billType'];
creationDate = json['creationDate'];
dueDate = json['dueDate'];
servicePeriodEnd = json['servicePeriodEnd'];
servicePeriodStart = json['servicePeriodStart'];
status = json['status'];
value = json['value'];
valueDisputed = json['valueDisputed'];
valueOpen = json['valueOpen'];
billURL = json['billURL'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['accountBalance'] = this.accountBalance;
if (this.address != null) {
data['address'] = this.address.toJson();
}
data['billDay'] = this.billDay;
data['billFrequency'] = this.billFrequency;
data['billNumber'] = this.billNumber;
data['billType'] = this.billType;
data['creationDate'] = this.creationDate;
data['dueDate'] = this.dueDate;
data['servicePeriodEnd'] = this.servicePeriodEnd;
data['servicePeriodStart'] = this.servicePeriodStart;
data['status'] = this.status;
data['value'] = this.value;
data['valueDisputed'] = this.valueDisputed;
data['valueOpen'] = this.valueOpen;
data['billURL'] = this.billURL;
return data;
}
}
class Address {
String street;
String complement;
String city;
String neighborhood;
bool isForcedAddress;
String state;
String zip;
String country;
String district;
Address(
{this.street,
this.complement,
this.city,
this.neighborhood,
this.isForcedAddress,
this.state,
this.zip,
this.country,
this.district});
Address.fromJson(Map<String, dynamic> json) {
street = json['street'];
complement = json['complement'];
city = json['city'];
neighborhood = json['neighborhood'];
isForcedAddress = json['isForcedAddress'];
state = json['state'];
zip = json['zip'];
country = json['country'];
district = json['district'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['street'] = this.street;
data['complement'] = this.complement;
data['city'] = this.city;
data['neighborhood'] = this.neighborhood;
data['isForcedAddress'] = this.isForcedAddress;
data['state'] = this.state;
data['zip'] = this.zip;
data['country'] = this.country;
data['district'] = this.district;
return data;
}
}
class模型类{
列表信息列表;
字符串transactionSequenceId;
字符串响应代码;
字符串响应消息;
模型类(
{this.billInfoList,
此.transactionSequenceId,
这是响应代码,
这是一种新的方法(responseMessage});
ModelClass.fromJson(映射json){
如果(json['billInfoList']!=null){
billInfoList=新列表();
json['billInfoList'].forEach((v){
add(新的billInfoList.fromJson(v));
});
}
transactionSequenceId=json['transactionSequenceId'];
responseCode=json['responseCode'];
responseMessage=json['responseMessage'];
}
映射到JSON(){
最终地图数据=新地图();
如果(this.billInfoList!=null){
data['billInfoList']=this.billInfoList.map((v)=>v.toJson()).toList();
}
数据['TransactionSequenceId']=this.TransactionSequenceId;
数据['ResponseCode']=this.ResponseCode;
数据['ResponseMessage']=此.ResponseMessage;
返回数据;
}
}
类BillInfoList{
国际账户余额;
地址;
国际比尔迪;
int billFrequency;
字符串编号;
int-billType;
弦肌;
字符串日期;
字符串服务周期结束;
字符串服务周期开始;
字符串状态;
双重价值;
int值有争议;
int-valueOpen;
字符串billURL;
账单信息列表(
{这是帐户余额,
这个地址,
这一天,
这是我的频率,
这是我的电话号码,
这是billType,
这是我的生日,
这个日期,
这,最后,,
这是一个开始,
这个,身份,,
这个,这个价值,,
这是有争议的,
这个是开放的,
这个文件名为.billURL});
fromJson(映射json){
accountBalance=json['accountBalance'];
地址=
json['address']!=null?新地址。fromJson(json['address']):null;
billDay=json['billDay'];
billFrequency=json['billFrequency'];
billNumber=json['billNumber'];
billType=json['billType'];
creationDate=json['creationDate'];
dueDate=json['dueDate'];
servicePeriodEnd=json['servicePeriodEnd'];
servicePeriodStart=json['servicePeriodStart'];
status=json['status'];
value=json['value'];
ValueConverted=json['ValueConverted'];
valueOpen=json['valueOpen'];
billURL=json['billURL'];
}
映射到JSON(){
最终地图数据=新地图();
数据['accountBalance']=this.accountBalance;
if(this.address!=null){
data['address']=this.address.toJson();
}
数据['billDay']=this.billDay;
数据['billFrequency']=this.billFrequency;
数据['billNumber']=此.billNumber;
数据['billType']=this.billType;
数据['creationDate']=此.creationDate;
数据['dueDate']=this.dueDate;
数据['servicePeriodEnd']=this.servicePeriodEnd;
数据['servicePeriodStart']=this.servicePeriodStart;
数据['status']=this.status;
数据['value']=this.value;
数据['ValueConvertive']=此.ValueConvertive;
数据['valueOpen']=this.valueOpen;
数据['billURL']=this.billURL;
返回数据;
}
}
班级地址{
弦街;;
字符串补码;
字符串城市;
弦邻域;
布勒是一件礼服;
字符串状态;
拉链;
弦国;
弦区;
地址(
{这条街,
这个,补充,,
这个城市,
这个社区,
这是我的礼服,
这个州,
这个.zip,
这个国家,
本区});
Address.fromJson(映射json){
street=json['street'];
complete=json['complete'];
city=json['city'];
邻里=json['neighborary'];
isForcedAddress=json['isForcedAddress'];
state=json['state'];
zip=json['zip'];
country=json['country'];
district=json['district'];
}
映射到JSON(){
最终地图数据=新地图();
数据['street']=this.street;
数据['complete']=this.complete;
数据['city']=本市;
数据['Neighborary']=this.neighborary;
数据['isForcedAddress']=this.isForcedAddress;
数据['state']=this.state;
数据['zip']=this.zip;
数据['country']=这个国家;
数据['district']=this.district;
返回数据;
}
}
您可以使用此链接从json生成模型类
导入'dart:convert';
BillInfoList billInfoListFromJson(String str)=>BillInfoList.fromJson(json.decode(str));
字符串billInfoListToJson(BillInfoList数据)=>json.encode(data.toJson());
类BillInfoList{
列表信息列表;
字符串transactionSequenceId;
字符串响应代码;
字符串响应消息;
账单信息列表({
这是billInfoList,
此.transactionSequenceId,
这是响应代码,
这是我的回答,
});
工厂BillInfoList.fromJson(映射json)=>BillInfoList(
billInfoList:List.from(json[“billInfoList”].map((x)=>BillInfoListElement.fromJson(x)),
transactionSequenceId:json[“transactionSequenceId”],
responseCode:json[“responseCode”],
responseMessage:json[“responseMessage”],
);
映射到JSON()=>{
“billInfoList”:List.from(billInfoList.map((x)=>x.toJson()),
“TransactionSequenceId”:TransactionSequenceId,
“响应代码”:响应代码,
“响应消息”:响应消息,
};
}
类BillInfoListElement{
int
import 'dart:convert';
BillInfoList billInfoListFromJson(String str) => BillInfoList.fromJson(json.decode(str));
String billInfoListToJson(BillInfoList data) => json.encode(data.toJson());
class BillInfoList {
List<BillInfoListElement> billInfoList;
String transactionSequenceId;
String responseCode;
String responseMessage;
BillInfoList({
this.billInfoList,
this.transactionSequenceId,
this.responseCode,
this.responseMessage,
});
factory BillInfoList.fromJson(Map<String, dynamic> json) => BillInfoList(
billInfoList: List<BillInfoListElement>.from(json["billInfoList"].map((x) => BillInfoListElement.fromJson(x))),
transactionSequenceId: json["TransactionSequenceId"],
responseCode: json["ResponseCode"],
responseMessage: json["ResponseMessage"],
);
Map<String, dynamic> toJson() => {
"billInfoList": List<dynamic>.from(billInfoList.map((x) => x.toJson())),
"TransactionSequenceId": transactionSequenceId,
"ResponseCode": responseCode,
"ResponseMessage": responseMessage,
};
}
class BillInfoListElement {
int accountBalance;
Address address;
int billDay;
int billFrequency;
String billNumber;
dynamic billType;
DateTime creationDate;
DateTime dueDate;
DateTime servicePeriodEnd;
DateTime servicePeriodStart;
String status;
double value;
int valueDisputed;
int valueOpen;
String billUrl;
BillInfoListElement({
this.accountBalance,
this.address,
this.billDay,
this.billFrequency,
this.billNumber,
this.billType,
this.creationDate,
this.dueDate,
this.servicePeriodEnd,
this.servicePeriodStart,
this.status,
this.value,
this.valueDisputed,
this.valueOpen,
this.billUrl,
});
factory BillInfoListElement.fromJson(Map<String, dynamic> json) => BillInfoListElement(
accountBalance: json["accountBalance"],
address: Address.fromJson(json["address"]),
billDay: json["billDay"],
billFrequency: json["billFrequency"],
billNumber: json["billNumber"],
billType: json["billType"],
creationDate: DateTime.parse(json["creationDate"]),
dueDate: DateTime.parse(json["dueDate"]),
servicePeriodEnd: DateTime.parse(json["servicePeriodEnd"]),
servicePeriodStart: DateTime.parse(json["servicePeriodStart"]),
status: json["status"],
value: json["value"].toDouble(),
valueDisputed: json["valueDisputed"],
valueOpen: json["valueOpen"],
billUrl: json["billURL"] == null ? null : json["billURL"],
);
Map<String, dynamic> toJson() => {
"accountBalance": accountBalance,
"address": address.toJson(),
"billDay": billDay,
"billFrequency": billFrequency,
"billNumber": billNumber,
"billType": billType,
"creationDate": "${creationDate.year.toString().padLeft(4, '0')}-${creationDate.month.toString().padLeft(2, '0')}-${creationDate.day.toString().padLeft(2, '0')}",
"dueDate": "${dueDate.year.toString().padLeft(4, '0')}-${dueDate.month.toString().padLeft(2, '0')}-${dueDate.day.toString().padLeft(2, '0')}",
"servicePeriodEnd": "${servicePeriodEnd.year.toString().padLeft(4, '0')}-${servicePeriodEnd.month.toString().padLeft(2, '0')}-${servicePeriodEnd.day.toString().padLeft(2, '0')}",
"servicePeriodStart": "${servicePeriodStart.year.toString().padLeft(4, '0')}-${servicePeriodStart.month.toString().padLeft(2, '0')}-${servicePeriodStart.day.toString().padLeft(2, '0')}",
"status": status,
"value": value,
"valueDisputed": valueDisputed,
"valueOpen": valueOpen,
"billURL": billUrl == null ? null : billUrl,
};
}
class Address {
String street;
String complement;
String city;
String neighborhood;
bool isForcedAddress;
String state;
String zip;
String country;
String district;
Address({
this.street,
this.complement,
this.city,
this.neighborhood,
this.isForcedAddress,
this.state,
this.zip,
this.country,
this.district,
});
factory Address.fromJson(Map<String, dynamic> json) => Address(
street: json["street"],
complement: json["complement"],
city: json["city"],
neighborhood: json["neighborhood"],
isForcedAddress: json["isForcedAddress"],
state: json["state"],
zip: json["zip"],
country: json["country"],
district: json["district"],
);
Map<String, dynamic> toJson() => {
"street": street,
"complement": complement,
"city": city,
"neighborhood": neighborhood,
"isForcedAddress": isForcedAddress,
"state": state,
"zip": zip,
"country": country,
"district": district,
};
}