Febin Johnson
Febin Johnson

Reputation: 384

Parsing Complex Json Data flutter

Im trying to parse a complex json data from server as follows

{
    "success": true,
    "data": {
        "user": {
            "_id": "6103a8036da3e840111c1365",
            "mobile_verified": true,
            "resend_otp": "no",
            "email": "[email protected]",
            "reg_email": "[email protected]",
            "device_details": [
                {
                    "device": "samsung",
                    "model": "SM-T295",
                    "platform": "Android",
                    "version": "9",
                    "currency": "INR",
                    "action": "Registration",
                    "app_version": "2.1.0"
                }
            ],
            "location_details": [
                {
                    "location_latitude": "null",
                    "location_longitude": "null",
                    "location_countryCode": "null",
                    "location_postalCode": "null",
                    "location_adminArea": "null",
                    "location_subAdminArea": "null",
                    "location_locality": "null",
                    "location_subLocality": "null",
                    "location_thoroughfare": "null",
                    "location_subThoroughfare": "null"
                }
            ],
            "device_id": [
                ""
            ],
            "user_valid_code": "O4IN",
            "leadstatus": "New",
            "daily_mail": true,
            "weekly_mail": true,
            "promotional_mail": true,
            "tr_valid": true,
            "user_type": "regular",
            "created_at": "1627645955218",
            "updated_at": "1628137882860",
            "google_contact_id": "",
            "otp": 7599,
            "email_verified": true,
            "accounts": [
                {
                    "_id": "6103a8036da3e840111c1365",
                    "name": "T108011",
                    "gender": "Boy",
                    "userid": "6103a8036da3e840111c1365",
                    "age": 6,
                    "payment_plan": "Free plan",
                    "created_by": "6103a8036da3e840111c1365",
                    "updated_by": "6103a8036da3e840111c1365",
                    "device_details": [
                        {
                            "device": "samsung",
                            "model": "SM-T295",
                            "platform": "Android",
                            "version": "9",
                            "currency": "INR",
                            "action": "Registration",
                            "app_version": "2.1.0"
                        }
                    ],
                    "valid_till": "2021-08-07 11:52:35",
                    "created_day": "5",
                    "account_module_available": "4",
                    "account_module_limit": "4",
                    "account_schedule_available": "4",
                    "account_schedule_limit": "4",
                    "account_daily_limit": "0",
                    "account_maximum_daily_limit": null,
                    "created_at": "1627645955225",
                    "updated_at": "1628124477089",
                    "next_sync_time": 1628215200,
                    "unfinished_module_count": "",
                    "expiry_date": 1628337155
                }
            ]
        },
        "otp_verified": true
    }
}

I have modeled this class for parsing this data

class UserDetails {
  String? id;
  bool? mobileVerified;
  String? resentOtp;
  String? email;
  String? regmail;
  List<UserDeviceDetails>? deviceDetails;
  List<UserLocationDetails>? locationDetails;
  List<String>? deviceId;
  String? userValidCode;
  String? leadstatus;
  bool? dailyMail;
  bool? weeklyMail;
  bool? promotionalMail;
  bool? trValid;
  String? userType;
  String? createdAt;
  String? updatedAt;
  String? hubspotid;
  String? googleContactId;
  String? malContent;
  int? otp;
  bool? emailVerified;
  List<UserAccount>? accounts;

  UserDetails();

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      "_id": id,
      "mobile_verified": mobileVerified,
      "resend_otp": resentOtp,
      "email": email,
      "reg_email": regmail,
      "device_details": deviceDetails,
      "location_details": locationDetails,
      "device_id": deviceId,
      "user_valid_code": userValidCode,
      "leadstatus": leadstatus,
      "daily_mail": dailyMail,
      "weekly_mail": weeklyMail,
      "promotional_mail": promotionalMail,
      "tr_valid": trValid,
      "user_type": userType,
      "created_at": createdAt,
      "updated_at": updatedAt,
      "hubspotid": hubspotid,
      "google_contact_id": googleContactId,
      "mal_content": malContent,
      "otp": otp,
      "email_verified": emailVerified,
      "accounts": accounts,
    };
    return map;
  }

   UserDetails.fromMap(Map<String, dynamic> map) {
    id = map["_id"];
    mobileVerified = map["mobile_verified"];
    resentOtp = map["resend_otp"];
    email = map["email"];
    regmail = map["reg_email"];
    deviceDetails =   List<UserDeviceDetails>.from(map["device_details"].map((x) => UserDeviceDetails.fromMap(x)));
    locationDetails = List<UserLocationDetails>.from(map["location_details"].map((x) => UserLocationDetails.fromMap(x)));
    deviceId =  List<String>.from(map["device_id"].map((x) => x));
    userValidCode = map["user_valid_code"];
    leadstatus = map["leadstatus"];
    dailyMail = map["daily_mail"];
    weeklyMail = map["weekly_mail"];
    promotionalMail = map["promotional_mail"];
    trValid = map["tr_valid"];
    userType = map["user_type"];
    createdAt = map["created_at"];
    updatedAt = map["updated_at"];
    hubspotid = map["hubspotid"];
    googleContactId = map["google_contact_id"];
    malContent = map["mal_content"];
    otp = map["otp"];
    emailVerified = map["email_verified"];
    accounts =  List<UserAccount>.from(map["accounts"].map((x) => UserAccount.fromMap(x)));
  }
}

Im getting response from to the objects i have created below. But whenever im trying to access the account information im getting [Instance of 'UserAccount']. Where i'm getting proper value of user.

if (response.statusCode == 200) {
      print("response body");
      ApiResponseBody body =
          ApiResponseBody.fromJson(convertResponseToJson(response));
      if (body.success == true) {
        print('this is bodydata : ${body.data.toString()}');
        UserDetails userDetails = UserDetails.fromMap(body.data["user"]);
        print('This is userdetails : ${userDetails.regmail} '); // gets result as [email protected]
        print('this is userAccount : ${userDetails.accounts}'); // gets [Instance of UserAccount']
      }

Am I missing something? I'm still learning flutter, any suggestions would be helpful

Upvotes: 0

Views: 46

Answers (1)

Asim Jawad
Asim Jawad

Reputation: 163

The info of accounts is in the form of the lists. you need to use userDetails.accounts[index].id or the any name of the variable for accessing it.

Upvotes: 1

Related Questions