作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 flutter 打我的jsonData
时遇到问题。这是我的json。
{
"status": "success",
"data": [
{
"id": "02ab1ef0-6fa9-11ea-ba0f-b1d475e9cef4",
"policy_name": "Third Party Insurance",
"description": "This is Third Party Insurance",
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 21:30:27",
"updated_at": "2020-03-26 21:30:27",
"insurance_policy_type": [
{
"id": "3c674550-6faa-11ea-b97c-9d7c2598dab0",
"insurance_policy_id": "02ab1ef0-6fa9-11ea-ba0f-b1d475e9cef4",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Third Party - Grade A",
"description": "Third Party - Grade A. Third Party - Grade A. Third Party - Grade A",
"benefit": "Benefit of Third Party - Grade A. This is going to be a long text",
"amount": 5000,
"commission": 300,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 21:39:13",
"updated_at": "2020-03-26 21:39:13"
},
{
"id": "4ec7a4a0-6faa-11ea-9739-77f0ed3fc12c",
"insurance_policy_id": "02ab1ef0-6fa9-11ea-ba0f-b1d475e9cef4",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Third Party - Grade B",
"description": "Third Party - Grade B. Third Party - Grade B. Third Party - Grade B",
"benefit": "Benefit of Third Party - Grade B. This is going to be a long text",
"amount": 8000,
"commission": 500,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 21:39:44",
"updated_at": "2020-03-26 21:39:44"
}
]
},
{
"id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
"policy_name": "Personal Accident Insurance",
"description": "This is Personal Accident Insurance",
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 21:30:51",
"updated_at": "2020-03-26 21:30:51",
"insurance_policy_type": [
{
"id": "aaf3ef10-6fb2-11ea-8c17-8de2cd67bf64",
"insurance_policy_id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Personal Accident - Grade A",
"description": "Personal Accident - Grade A. Personal Accident - Grade A. Personal Accident - Grade A",
"benefit": "Benefit of Personal Accident - Grade A. This is going to be a long text",
"amount": 4000,
"commission": 500,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 22:39:35",
"updated_at": "2020-03-26 22:39:35"
},
{
"id": "b4835d00-6fb2-11ea-a48f-c3481acd8970",
"insurance_policy_id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Personal Accident - Grade B",
"description": "Personal Accident - Grade B. Personal Accident - Grade B. Personal Accident - Grade B",
"benefit": "Benefit of Personal Accident - Grade B. This is going to be a long text",
"amount": 4000,
"commission": 500,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 22:39:51",
"updated_at": "2020-03-26 22:39:51"
},
{
"id": "c6d84e60-6fb2-11ea-a773-497e66c9ab29",
"insurance_policy_id": "10e31df0-6fa9-11ea-962f-e5df8d00bea3",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Personal Accident - Grade C",
"description": "Personal Accident - Grade C. Personal Accident - Grade C. Personal Accident - Grade C",
"benefit": "Benefit of Personal Accident - Grade C. This is going to be a long text",
"amount": 10000,
"commission": 1000,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 22:40:21",
"updated_at": "2020-03-26 22:40:21"
}
]
},
{
"id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
"policy_name": "Household Insurance",
"description": "This is Household Insurance",
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 21:31:07",
"updated_at": "2020-03-26 21:34:17",
"insurance_policy_type": [
{
"id": "016fa680-6fb3-11ea-93b7-3fdae3d5875a",
"insurance_policy_id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Household Insurance - Grade D",
"description": "Household Insurance - Grade D. Household Insurance - Grade D. Household Insurance - Grade D",
"benefit": "Benefit of Household Insurance - Grade D. This is going to be a long text",
"amount": 9000,
"commission": 1000,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 22:42:00",
"updated_at": "2020-03-26 22:42:00"
},
{
"id": "13a62720-6fb3-11ea-8842-ed9732bb7c86",
"insurance_policy_id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Household Insurance - Grade K",
"description": "Household Insurance - Grade K. Household Insurance - Grade K. Household Insurance - Grade K",
"benefit": "Benefit of Household Insurance - Grade K. This is going to be a long text",
"amount": 7500,
"commission": 600,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 22:42:30",
"updated_at": "2020-03-26 22:42:30"
},
{
"id": "f4ceb490-6fb2-11ea-9b04-a1229a2e2128",
"insurance_policy_id": "1a727fc0-6fa9-11ea-a43f-b52dc75efa00",
"insurance_company_id": "07a53f10-6fa8-11ea-bb6d-89fb742a644e",
"policy_type_name": "Household Insurance - Grade C",
"description": "Household Insurance - Grade C. Household Insurance - Grade C. Household Insurance - Grade C",
"benefit": "Benefit of Household Insurance - Grade C. This is going to be a long text",
"amount": 9000,
"commission": 1000,
"status": 1,
"deleted_at": null,
"created_at": "2020-03-26 22:41:39",
"updated_at": "2020-03-26 22:41:39"
}
]
}
]
}
class Insurance {
String status;
List<Data> data;
Insurance({this.status, this.data});
Insurance.fromJson(Map<String, dynamic> json) {
status = json['status'];
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['status'] = this.status;
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String id;
String policyName;
String description;
int status;
var deletedAt;
String createdAt;
String updatedAt;
List<InsurancePolicyType> insurancePolicyType;
Data(
{this.id,
this.policyName,
this.description,
this.status,
this.deletedAt,
this.createdAt,
this.updatedAt,
this.insurancePolicyType});
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
policyName = json['policy_name'];
description = json['description'];
status = json['status'];
deletedAt = json['deleted_at'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
if (json['insurance_policy_type'] != null) {
insurancePolicyType = new List<InsurancePolicyType>();
json['insurance_policy_type'].forEach((v) {
insurancePolicyType.add(new InsurancePolicyType.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['policy_name'] = this.policyName;
data['description'] = this.description;
data['status'] = this.status;
data['deleted_at'] = this.deletedAt;
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
if (this.insurancePolicyType != null) {
data['insurance_policy_type'] =
this.insurancePolicyType.map((v) => v.toJson()).toList();
}
return data;
}
}
class InsurancePolicyType {
String id;
String insurancePolicyId;
String insuranceCompanyId;
String policyTypeName;
String description;
String benefit;
int amount;
int commission;
int status;
Null deletedAt;
String createdAt;
String updatedAt;
InsurancePolicyType(
{this.id,
this.insurancePolicyId,
this.insuranceCompanyId,
this.policyTypeName,
this.description,
this.benefit,
this.amount,
this.commission,
this.status,
this.deletedAt,
this.createdAt,
this.updatedAt});
InsurancePolicyType.fromJson(Map<String, dynamic> json) {
id = json['id'];
insurancePolicyId = json['insurance_policy_id'];
insuranceCompanyId = json['insurance_company_id'];
policyTypeName = json['policy_type_name'];
description = json['description'];
benefit = json['benefit'];
amount = json['amount'];
commission = json['commission'];
status = json['status'];
deletedAt = json['deleted_at'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['insurance_policy_id'] = this.insurancePolicyId;
data['insurance_company_id'] = this.insuranceCompanyId;
data['policy_type_name'] = this.policyTypeName;
data['description'] = this.description;
data['benefit'] = this.benefit;
data['amount'] = this.amount;
data['commission'] = this.commission;
data['status'] = this.status;
data['deleted_at'] = this.deletedAt;
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
return data;
}
}
List<InsurancePolicyType> insurancePolicyType;
List<Data> insuranceDataList = [];
...
insuranceDataList = List<Data>.from(
responseJson["data"].map((i) => Data.fromJson(i)));
for (var i = 0; i < insuranceDataList.length; i++) {
insurancePolicyType = List<InsurancePolicyType>.from(
responseJson["data"][i]["insurance_policy_type"].map((i) =>InsurancePolicyType.fromJson(i)));
List<InsurancePolicyType> insurancePolicyType
?我需要获取该列表,但我无法。
最佳答案
在这种情况下,写 setter/getter 应该没问题
class Insurance {
String status;
List<Data> data;
List<InsurancePolicyType> get getAllInsurancePolicies {
List<InsurancePolicyType> fetchedList = List();
for (var d in data) {
fetchedList.addAll(d.insurancePolicyType);
}
return fetchedList;
}
Insurance({this.status, this.data});
Insurance.fromJson(Map<String, dynamic> json) {
status = json['status'];
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['status'] = this.status;
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
关于flutter - 如何从 flutter 中的数据JSON列表解析列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60948803/
我是一名优秀的程序员,十分优秀!