我在Flutter中有一个Json解析项目,Json如下:
{
"Dependents":[
{
"Name": "Kim",
"Relationship": "Parent",
"Entitlements": [
{
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"IP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"Dental": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"Optical": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"Maternity": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
}
]
},
{
"Name": "Tim",
"Relationship": "Spouse",
"Entitlements": [
{
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"IP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"Maternity": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
}
]
},
{
"Name": "Lim",
"Relationship": "Child",
"Entitlements": [
{
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"Dental": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"Optical": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"Maternity": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
}
]
},
{
"Name": "Xim",
"Relationship": "Child",
"Entitlements": [
{
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
},
{
"IP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
如您所见,在受抚养人下有多个用户,在这些用户下有他们自己的权利。
我目前遇到的问题是:
由于每个用户都有与之关联的不同的权利,因此我如何循环浏览这些权利以打印出所有地图。
请协助。
您可以使用以下代码段:
void iterateJson(String jsonStr) {
Map<String, dynamic> myMap = json.decode(jsonStr);
List<dynamic> entitlements = myMap["Dependents"][0]["Entitlements"];
entitlements.forEach((entitlement) {
(entitlement as Map<String, dynamic>).forEach((key, value) {
print(key);
(value as Map<String, dynamic>).forEach((key2, value2) {
print(key2);
print(value2);
});
});
});
}
Run Code Online (Sandbox Code Playgroud)
尽管如果您不关心订单,可以通过删除列表并仅保留一张地图来简化“权利”字段:
"Entitlements": {
"GP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
},
"OPS": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
},
"IP": {
"Entitlement": "10000",
"Utilisation": "500",
"Balance": "9500"
}
}
Run Code Online (Sandbox Code Playgroud)
你有信息:http : //cogitas.net/parse-json-dart-flutter/
页面示例:
void _parseJsonForCrossword(String jsonString) {
Map decoded = JSON.decode(jsonString);
String name = decoded['name'];
print(name);
int id = decoded['id'];
print(id.toString());
for (var word in decoded['across']) {
print(word['number'].toString());
print(word['word']);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3965 次 |
| 最近记录: |