【问题标题】:Flutter: JSON Loop颤振:JSON 循环
【发布时间】:2019-02-01 17:32:52
【问题描述】:

我在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"
              }
            }
        ]
      }
    ]
}

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

我目前遇到的问题是:

由于每个用户都有不同的权利与他们关联,我如何循环访问权利以打印出其下的所有地图。

请提供帮助。

【问题讨论】:

  • 一个非常广泛的问题,但在这里你可能会找到一些答案flutter.io/json

标签: json dart flutter


【解决方案1】:

您可以使用以下 sn-p:

  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);
        });
      });
    });
  }

如果您不关心订单,则可以通过删除列表并仅保留一张地图来简化“权利”字段:

"Entitlements": {
              "GP": {
                "Entitlement": "10000",
                "Utilisation": "500",
                "Balance": "9500"
              },
              "OPS": {
                "Entitlement": "10000",
                "Utilisation": "500",
                "Balance": "9500"
              },
              "IP": {
                "Entitlement": "10000",
                "Utilisation": "500",
                "Balance": "9500"
              }
            }

【讨论】:

  • 谢谢。我会试试这个。但是我在 iterateJson 方法中在哪里设置 url?
  • 因为我在解析这个 JSON 的类中使用了未来的构建器。
  • 好吧,在您的问题中,您要求一种迭代entitlements 的方法,这是一种方法。我们缺乏对您申请的看法。如果您的 future 返回 json 字符串,您可以在 future 构建器中解析字符串并从那里构建您的小部件。
【解决方案2】:

那里有你的信息: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']);
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2021-03-23
    • 2021-02-25
    • 2020-05-30
    • 2022-01-14
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多