【发布时间】:2019-01-24 05:48:26
【问题描述】:
我这里有一个嵌套的 JSON api:
[
{
"Employee": {
"Name": "Michael Jackson",
"Identification": "881228145031",
"Company": "Test Corporate",
"DateOfBirth": "1988-12-28",
"Entitlements": {
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
},
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
},
"IP": {
"Entitlement": "50000",
"Utilisation": "17000",
"Balance": "33000"
},
"Dental": {
"Entitlement": "0.00",
"Utilisation": "0.00",
"Balance": "0.00"
},
"Optical": {
"Entitlement": "500",
"Utilisation": "0.00",
"Balance": "500"
},
"EHS": {
"Entitlement": "1000",
"Utilisation": "250",
"Balance": "750"
}
}
},
"Dependents": [
{
"Name": "Kim",
"Relationship": "Parent",
"Entitlements": {
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
},
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
},
"IP": {
"Entitlement": "50000",
"Utilisation": "17000",
"Balance": "33000"
},
"Dental": {
"Entitlement": "800",
"Utilisation": "200",
"Balance": "600"
},
"Optical": {
"Entitlement": "500",
"Utilisation": "0.00",
"Balance": "500"
},
"EHS": {
"Entitlement": "1000",
"Utilisation": "250",
"Balance": "750"
}
}
},
{
"Name": "Tim",
"Relationship": "Spouse",
"Entitlements": {
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
},
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
}
}
]
}
]
如您所见,JSON 文件在 Employee 和 Dependents 中都有相同的嵌套,称为 Entitlements,其中包含一些 Map其中。
Employee和Dependents的基本模型类如下:
crm_single_user_model.dart(员工模型)
class SingleUser{
final String name, identification, company, dob;
SingleUser({this.name, this.identification, this.company, this.dob});
factory SingleUser.fromJson(Map<String, dynamic> ujson){
return SingleUser(
name: ujson["Name"].toString(),
identification: ujson["Identification"].toString(),
company: ujson["Company"].toString(),
dob: ujson["DateOfBirth"].toString()
);
}
}
crm_dependent_list_model.dart(家属模型)
class DependantModel{
String name;
String relationship;
DependantModel({this.name, this.relationship});
factory DependantModel.fromJson(Map<String, dynamic> depjson){
return DependantModel(
name: depjson["Name"].toString(),
relationship: depjson["Relationship"].toString()
);
}
}
我的问题是 如何为权利创建模型类?
我似乎无法想出一个解决方案来创建一个在地图中包含大量地图的模型类。
非常感谢您在这件事上的帮助。
【问题讨论】:
-
保存到地图中
-
您可以使用映射并迭代映射值来解析数组。如果您需要示例代码,请告诉我。我可以解释如何将 JSON 响应转换为 Java 的 POJO 等价物
-
是的 Jaswant,如果不麻烦的话,我可以使用示例代码。
-
@JaswantSingh 抱歉忘记标记你的名字。
-
您可能需要检查 json_resolve dev.to/onmyway133/how-to-resolve-deep-json-object-in-dart-5c5l