Home > front end >  How to parse JSON Array of Array of objects
How to parse JSON Array of Array of objects

Time:10-20

basically the issue is i am getting cities array and in the cities array i have multiple cities and each city also contain array i need model for this json response i am using "https://app.quicktype.io/"

{
        "code": "0",
        "message": "Success!",
        "data": {
            "firstName": "hello",
            "lastName": "world",
            "mobile1": "123456789",
            "mobile2": "123456789",
            "cities": [
                {
                    "Dubai": [
                        {
                            "id": 17,
                            "value": "Dubai",
                            "selected": false
                        }
                    ]
                },
                {
                    "Ajman": [
                        {
                            "id": 29,
                            "value": "Ajman",
                            "selected": false
                        }
                    ]
                },
                {
                    "Fujairah": [
                        {
                            "id": 30,
                            "value": "Fujairah",
                            "selected": false
                        }
                    ]
                },
                {
                    "Ras Al Khaimah": [
                        {
                            "id": 31,
                            "value": "Ras Al Khaimah",
                            "selected": false
                        }
                    ]
                },
                {
                    "Um Ul Quwein": [
                        {
                            "id": 32,
                            "value": "Umm Al Quwein",
                            "selected": false
                        }
                    ]
                },
                {
                    "AbuDhabi": [
                        {
                            "id": 33,
                            "value": "Al Ain",
                            "selected": false
                        },
                        {
                            "id": 34,
                            "value": "Abu Dhabi",
                            "selected": false
                        }
                    ]
                },
                {
                    "Sharjah": [
                        {
                            "id": 35,
                            "value": "Sharjah",
                            "selected": true
                        }
                    ]
                }
            ],
            "picture": "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/640px-Image_created_with_a_mobile_phone.png"
        }
    }

CodePudding user response:

If you are in control of the API I'd highly suggest restructuring the response in such a way that you don't have any dynamic keys. It's much more conventional and also easier to work with. For your example you could change this part

            {
                "AbuDhabi": [
                    {
                        "id": 33,
                        "value": "Al Ain",
                        "selected": false
                    },
                    {
                        "id": 34,
                        "value": "Abu Dhabi",
                        "selected": false
                    }
                ]
            }

to

            {
                "name" : "AbuDhabi",
                "cities" : [
                    {
                        "id": 33,
                        "value": "Al Ain",
                        "selected": false
                    },
                    {
                        "id": 34,
                        "value": "Abu Dhabi",
                        "selected": false
                    }
                ]
            }

And then maybe rename the outer "cities" to "emirates"

CodePudding user response:

Use this JsontoDart to convert json response to Model.

CodePudding user response:

You can use the following website to convert your JSON to POJO class -

https://javiercbk.github.io/json_to_dart/

Your Response class will be like this -

class Response{
  String? code;
  String? message;
  Data? data;

  Response({this.code, this.message, this.data});

  Response.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    message = json['message'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['code'] = this.code;
    data['message'] = this.message;
    if (this.data != null) {
      data['data'] = this.data!.toJson();
    }
    return data;
  }
}

class Data {
  String? firstName;
  String? lastName;
  String? mobile1;
  String? mobile2;
  List<Cities>? cities;
  String? picture;

  Data(
      {this.firstName,
      this.lastName,
      this.mobile1,
      this.mobile2,
      this.cities,
      this.picture});

  Data.fromJson(Map<String, dynamic> json) {
    firstName = json['firstName'];
    lastName = json['lastName'];
    mobile1 = json['mobile1'];
    mobile2 = json['mobile2'];
    if (json['cities'] != null) {
      cities = <Cities>[];
      json['cities'].forEach((v) {
        cities!.add(new Cities.fromJson(v));
      });
    }
    picture = json['picture'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['firstName'] = this.firstName;
    data['lastName'] = this.lastName;
    data['mobile1'] = this.mobile1;
    data['mobile2'] = this.mobile2;
    if (this.cities != null) {
      data['cities'] = this.cities!.map((v) => v.toJson()).toList();
    }
    data['picture'] = this.picture;
    return data;
  }
}

class Cities {
  List<Dubai>? dubai;
  List<Ajman>? ajman;
  List<Fujairah>? fujairah;
  List<RasAlKhaimah>? rasAlKhaimah;
  List<UmUlQuwein>? umUlQuwein;
  List<AbuDhabi>? abuDhabi;
  List<Sharjah>? sharjah;

  Cities(
      {this.dubai,
      this.ajman,
      this.fujairah,
      this.rasAlKhaimah,
      this.umUlQuwein,
      this.abuDhabi,
      this.sharjah});

  Cities.fromJson(Map<String, dynamic> json) {
    if (json['Dubai'] != null) {
      dubai = <Dubai>[];
      json['Dubai'].forEach((v) {
        dubai!.add(new Dubai.fromJson(v));
      });
    }
    if (json['Ajman'] != null) {
      ajman = <Ajman>[];
      json['Ajman'].forEach((v) {
        ajman!.add(new Ajman.fromJson(v));
      });
    }
    if (json['Fujairah'] != null) {
      fujairah = <Fujairah>[];
      json['Fujairah'].forEach((v) {
        fujairah!.add(new Fujairah.fromJson(v));
      });
    }
    if (json['Ras Al Khaimah'] != null) {
      rasAlKhaimah = <RasAlKhaimah>[];
      json['Ras Al Khaimah'].forEach((v) {
        rasAlKhaimah!.add(new RasAlKhaimah.fromJson(v));
      });
    }
    if (json['Um Ul Quwein'] != null) {
      umUlQuwein = <UmUlQuwein>[];
      json['Um Ul Quwein'].forEach((v) {
        umUlQuwein!.add(new UmUlQuwein.fromJson(v));
      });
    }
    if (json['AbuDhabi'] != null) {
      abuDhabi = <AbuDhabi>[];
      json['AbuDhabi'].forEach((v) {
        abuDhabi!.add(new AbuDhabi.fromJson(v));
      });
    }
    if (json['Sharjah'] != null) {
      sharjah = <Sharjah>[];
      json['Sharjah'].forEach((v) {
        sharjah!.add(new Sharjah.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.dubai != null) {
      data['Dubai'] = this.dubai!.map((v) => v.toJson()).toList();
    }
    if (this.ajman != null) {
      data['Ajman'] = this.ajman!.map((v) => v.toJson()).toList();
    }
    if (this.fujairah != null) {
      data['Fujairah'] = this.fujairah!.map((v) => v.toJson()).toList();
    }
    if (this.rasAlKhaimah != null) {
      data['Ras Al Khaimah'] =
          this.rasAlKhaimah!.map((v) => v.toJson()).toList();
    }
    if (this.umUlQuwein != null) {
      data['Um Ul Quwein'] = this.umUlQuwein!.map((v) => v.toJson()).toList();
    }
    if (this.abuDhabi != null) {
      data['AbuDhabi'] = this.abuDhabi!.map((v) => v.toJson()).toList();
    }
    if (this.sharjah != null) {
      data['Sharjah'] = this.sharjah!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Dubai {
  int? id;
  String? value;
  bool? selected;

  Dubai({this.id, this.value, this.selected});

  Dubai.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    value = json['value'];
    selected = json['selected'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['value'] = this.value;
    data['selected'] = this.selected;
    return data;
  }
}
  • Related