在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)));
因此,在每个索引中都有另一个列表,我需要将其保存在InsurancePolicyType对象中。这就是我试图获取的数据。如何在代码中获取列表
?我需要去拿那张名单,但我没能去。
在这种情况下写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;
}
}
问题内容: 我想创建一个新闻应用程序,并且已将newsapi.org用作源。 我正在尝试获取Http库提供的JSON数据。 我在下面提供了完整的代码。 它不会给我任何错误,但不会加载任何数据,当我打印数据时,它可以打印所有内容,但无法显示。 我不是什么问题,但是我所有的项目都已停止解决此问题。 我正在寻找此代码的解决方案,因为它不起作用。 网络助手: 问题答案: 问题在于显示文章。yaa也正如@I
问题内容: 我正在使用dart包json_serializable进行json序列化。查看flutter文档,它显示了如何反序列化单个对象,如下所示: 但是,我对dart不够熟悉,无法弄清楚如何对项目列表而不是单个实例执行相同的操作。 问题答案: 好吧,您的服务将处理作为地图的响应正文,或相应地处理地图列表。根据您所拥有的代码,您要核算1件商品。 如果响应主体是可迭代的,那么,如果我正确理解了您的
问题内容: 如何在Flutter中解码JSON? 问题很简单,但答案至少对我来说不是。 我有一个使用大量JSON字符串的项目。基本上,应用程序和服务器之间的整个通信是通过JSON进行的。 我一直在使用它来处理它,但今天我更新了Flutter核心(0.5.8-pre.178),并且JSON.decode不再可用。 我去了Flutter Docs寻求帮助,但仍然说要使用JSON.decode。 那么,
我一直在努力解决这个问题,无法使用以下代码解析firebase实时数据库中的数据。我什么都试过了,但我做不到。下面是必要文件的代码。我正在使用提供程序进行状态管理。 我的供应商代码: 我调用的小部件: 我的模型课: 我得到以下错误: 如果有人能帮忙,我将非常感激,它已经调试了很长时间...
问题内容: 我是Jquery,Ajax和JSON的新手。我在解析Json数据时遇到问题。我在Stackoverflow上遇到了很多问题 解析HTML表的JSON对象 访问/处理(嵌套的)对象,数组或JSON 在JavaScript中解析JSON? 如何在JQuery中解析此JSON对象? 还有很多… 我仍然无法解析Json数据。 我的Jquery看起来像: 我已经尝试了所有组合来解析此数据,但是j
问题内容: 我正在使用一个API,该API返回如下所示的JSON 在Swift中,我使用两个函数来获取并解析JSON 然后我用 可以很好地解析JSON。当我打印出 我得到了数组的所有内容。但是,我无法访问每个单独的索引。我很肯定这是一个数组,因为我之间 返回正确的长度。但是,如果我尝试通过使用访问单个索引 XCode关闭语法高亮显示并给我以下信息: 并且代码将无法编译。 这是XCode 6的错误,