Flutter: JSON Loop

前端 未结 2 1294
孤独总比滥情好
孤独总比滥情好 2021-01-02 04:18

I have a Json parsing project in Flutter and the Json is as follows:

{
  \"Dependents\":[
      {
        \"Name\": \"Kim\",
        \"Relationship\": \"Pare         


        
2条回答
  •  耶瑟儿~
    2021-01-02 04:41

    You could use the following snippet:

      void iterateJson(String jsonStr) {
        Map myMap = json.decode(jsonStr);
        List entitlements = myMap["Dependents"][0]["Entitlements"];
        entitlements.forEach((entitlement) {
          (entitlement as Map).forEach((key, value) {
            print(key);
            (value as Map).forEach((key2, value2) {
              print(key2);
              print(value2);
            });
          });
        });
      }
    

    Although you could possibly simplify a little the 'entitlements' field if you don't care about the order by removing the list and leaving just one map:

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

提交回复
热议问题