Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: JSON Loop

Tags:

json

flutter

dart

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

{
  "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"
              }
            }
        ]
      }
    ]
}

As you can see, under the Dependents there are multiple users and under those users they have their own Entitlements.

The problem I am having currently is:

How do I loop through the Entitlements to print out all the maps under that since every user has different Entitlements associated with them.

Kindly assist.

like image 313
Asyraf Dayan Avatar asked Aug 27 '18 07:08

Asyraf Dayan


People also ask

How do I display JSON data in flutter?

Step 1: Create a project in Vs code, And remove the default code. Step 2: Before writing the code just add the HTTP plugin in your pubspec yaml file. Step 3: In main. dart file call the main() function , inside it run the runApp( ) method and give it an App (MyApp).


1 Answers

You could use the following snippet:

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

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"
              }
            }
like image 155
chemamolins Avatar answered Sep 27 '22 22:09

chemamolins