这是JSON响应,我需要将数据检索到混乱状态。 在投射详细信息时出错。
{code": 200,
"message": "Success",
"data": [
{
"fname": "LG",
"lname": "Electronics",
"email": "lg@gmail.com",
"address": "Girish Cold Drinks, Chimanlal Girdharlal Road, Mithakhali, Navrangpura, Ahmedabad, Gujarat, India",
}
}
这是客户类别
class Customer {
int code;
String message;
List<Data> data;
Customer({this.code, this.message, this.data});
Customer.fromJson(Map<String, dynamic> json) {
code = json['code'];
message = json['message'];
if (json['data'] != null) {
data = new List<Data>();
json['data'].forEach((v) {
data.add(new Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['code'] = this.code;
data['message'] = this.message;
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String fname;
String lname;
String email;
String address;
Data(
{
this.fname,
this.lname,
this.email,
this.address,
});
Data.fromJson(Map<String, dynamic> json) {
fname = json['fname'];
lname = json['lname'];
email = json['email'];
address = json['address'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['fname'] = this.fname;
data['lname'] = this.lname;
data['email'] = this.email;
data['address'] = this.address;
return data;
}
}
我在这里遇到投放错误
final jsonResponse = json.decode(response.body).cast<Map<String, dynamic>>();
List<Customer> listOfUsers = jsonResponse.map<Customer>((json) async {
return Customer.fromJson(json);
}).toList()
错误:Flutter:InternalLinkedHashMap'没有具有匹配参数的实例方法'cast'
答案 0 :(得分:0)
您可以使用Customer.fromJson(json.decode(response.body))
。
对于任何复杂的JSON解析,请使用quicktype.io。
更新: 您的pojo课堂将是这样的:
import 'dart:convert';
Customer customerFromJson(String str) => Customer.fromJson(json.decode(str));
String customerToJson(Customer data) => json.encode(data.toJson());
class Customer {
int code;
String message;
List<Data> data;
Customer({
this.code,
this.message,
this.data,
});
factory Customer.fromJson(Map<String, dynamic> json) => Customer(
code: json["code"],
message: json["message"],
data: List<Data>.from(json["data"].map((x) => Data.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"code": code,
"message": message,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Data {
String fname;
String lname;
String email;
String address;
Data({
this.fname,
this.lname,
this.email,
this.address,
});
factory Data.fromJson(Map<String, dynamic> json) => Data(
fname: json["fname"],
lname: json["lname"],
email: json["email"],
address: json["address"],
);
Map<String, dynamic> toJson() => {
"fname": fname,
"lname": lname,
"email": email,
"address": address,
};
}
答案 1 :(得分:0)
在Customer
中尝试此操作,
class Customer {
int code;
String message;
List<Data> data;
Customer({this.code, this.message, this.data});
factory Customer.fromJson(Map<String, dynamic> json) {
code = json['code'];
message = json['message'];
var list = json['data'] as List;
List<Data> dataList =
list != null ? list.map((i) => Data.fromJson(i)).toList() : [];
retrun Customer(code:code,message,data:dataList)
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['code'] = this.code;
data['message'] = this.message;
if (this.data != null) {
data['data'] = this.data;
}
return data;
}
}
Data
正在关注
class Data {
String fname;
String lname;
String email;
String address;
Data({
this.fname,
this.lname,
this.email,
this.address,
});
factory Data.fromJson(Map<String, dynamic> json) {
fname = json['fname'];
lname = json['lname'];
email = json['email'];
address = json['address'];
return Data(fname:fname,lname:lname,email:email,address:address);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['fname'] = this.fname;
data['lname'] = this.lname;
data['email'] = this.email;
data['address'] = this.address;
return data;
}
}