3

I have a nested JSON api here:

[
  {
    "Employee": {
      "Name": "Michael Jackson",
      "Identification": "881228145031",
      "Company": "Test Corporate",
      "DateOfBirth": "1988-12-28",
      "Entitlements": {
        "GP": {
          "Entitlement": "10000",
          "Utilisation": "500",
          "Balance": "9500"
        },
        "OPS": {
          "Entitlement": "10000",
          "Utilisation": "500",
          "Balance": "9500"
        },
        "IP": {
          "Entitlement": "50000",
          "Utilisation": "17000",
          "Balance": "33000"
        },
        "Dental": {
          "Entitlement": "0.00",
          "Utilisation": "0.00",
          "Balance": "0.00"
        },
        "Optical": {
          "Entitlement": "500",
          "Utilisation": "0.00",
          "Balance": "500"
        },
        "EHS": {
          "Entitlement": "1000",
          "Utilisation": "250",
          "Balance": "750"
        }
      }
    },
    "Dependents": [
      {
        "Name": "Kim",
        "Relationship": "Parent",
        "Entitlements": {
          "GP": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          },
          "OPS": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          },
          "IP": {
            "Entitlement": "50000",
            "Utilisation": "17000",
            "Balance": "33000"
          },
          "Dental": {
            "Entitlement": "800",
            "Utilisation": "200",
            "Balance": "600"
          },
          "Optical": {
            "Entitlement": "500",
            "Utilisation": "0.00",
            "Balance": "500"
          },
          "EHS": {
            "Entitlement": "1000",
            "Utilisation": "250",
            "Balance": "750"
          }
        }
      },
      {
        "Name": "Tim",
        "Relationship": "Spouse",
        "Entitlements": {
          "GP": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          },
          "OPS": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          }
        }
      }
    ]
  }
]

As you can see, the JSON file has the same nest in both Employee and Dependents called Entitlements and it has a few Maps inside of it.

The basic Model class for both Employee and Dependents are as follows:

crm_single_user_model.dart (Model for Employee)

class SingleUser{

  final String name, identification, company, dob;

  SingleUser({this.name, this.identification, this.company, this.dob});

  factory SingleUser.fromJson(Map<String, dynamic> ujson){

    return SingleUser(
      name: ujson["Name"].toString(),
      identification: ujson["Identification"].toString(),
      company: ujson["Company"].toString(),
      dob: ujson["DateOfBirth"].toString()
    );
  }
}

crm_dependent_list_model.dart(Model for Dependents)

class DependantModel{

  String name;
  String relationship;

  DependantModel({this.name, this.relationship});

  factory DependantModel.fromJson(Map<String, dynamic> depjson){

    return DependantModel(
      name: depjson["Name"].toString(),
      relationship: depjson["Relationship"].toString()
    );
  }
}

My question is How do i make a model class for the Entitlements?

I can't seem to come up with a solution to creating a model class with lots of maps inside of a map.

Your help is much appreciated in this matter.

5
  • just save it into a map Commented Aug 17, 2018 at 13:34
  • You can use a map and iterate map values to parse an array. If you want an example code, please let me know. I can explain how to convert a JSON response into a POJO equivalent of Java Commented Aug 17, 2018 at 14:21
  • Yes Jaswant, I could use an example code if thats not too much of a hassle. Commented Aug 18, 2018 at 1:53
  • @JaswantSingh sorry forgot to tag your name. Commented Aug 20, 2018 at 1:58
  • You may want to check json_resolve dev.to/onmyway133/how-to-resolve-deep-json-object-in-dart-5c5l Commented Apr 8, 2019 at 21:53

1 Answer 1

1

try to see that example

https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51

class Product {
  final int id;
  final String name;
  final List<Image> images;

  Product({this.id, this.name, this.images});
}

class Image {
  final int imageId;
  final String imageName;

  Image({this.imageId, this.imageName});
}
Sign up to request clarification or add additional context in comments.

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.