首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >颤动: JSON循环

颤动: JSON循环
EN

Stack Overflow用户
提问于 2018-08-27 15:53:29
回答 2查看 20.4K关注 0票数 12

我在Flutter中有一个Json解析项目,Json如下所示:

代码语言:javascript
复制
{
  "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"
              }
            }
        ]
      }
    ]
}

如您所见,在Dependents下有多个用户,而在这些用户下,他们有自己的授权

我目前遇到的问题是:

由于每个用户都有不同的授权与之关联,我该如何遍历授权以打印出其下的所有地图。

敬请协助。

EN

回答 2

Stack Overflow用户

发布于 2018-08-27 19:17:18

您可以使用以下代码片段:

代码语言:javascript
复制
  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);
        });
      });
    });
  }

不过,如果您不关心顺序,可以通过删除列表并只留下一个映射来简化“授权”字段:

代码语言:javascript
复制
"Entitlements": {
              "GP": {
                "Entitlement": "10000",
                "Utilisation": "500",
                "Balance": "9500"
              },
              "OPS": {
                "Entitlement": "10000",
                "Utilisation": "500",
                "Balance": "9500"
              },
              "IP": {
                "Entitlement": "10000",
                "Utilisation": "500",
                "Balance": "9500"
              }
            }
票数 20
EN

Stack Overflow用户

发布于 2018-08-27 19:37:27

您在那里有信息:http://cogitas.net/parse-json-dart-flutter/

来自页面的示例:

代码语言:javascript
复制
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']);
 }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52034895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档