Flutter: JSON Loop

前端 未结 2 1281
孤独总比滥情好
孤独总比滥情好 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:31

    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']);
     }
    }
    
    0 讨论(0)
  • 2021-01-02 04:41

    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"
                  }
                }
    
    0 讨论(0)
提交回复
热议问题