19

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.

1
  • A very broad question, but here you'll probably find some answers flutter.io/json Commented Aug 27, 2018 at 10:35

2 Answers 2

30

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"
              }
            }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I will give this a try. But where do i set the url in the iterateJson method?
Because I am using a future builder in the class that parses this JSON.
Well, in your question you asked for a way to iterate the entitlements, and this is one way to do it. We lack perspective of your application. If your future returns the json string you can parse the string inside the future builder and build your widgets from there.
7

You have information there : http://cogitas.net/parse-json-dart-flutter/

Example from the page :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.