【问题标题】:Parsing Complex Json Data flutter解析复杂的 Json 数据颤动
【发布时间】:2021-10-10 02:56:40
【问题描述】:

我正在尝试从服务器解析复杂的 json 数据,如下所示

{
    "success": true,
    "data": {
        "user": {
            "_id": "6103a8036da3e840111c1365",
            "mobile_verified": true,
            "resend_otp": "no",
            "email": "test1@gmail.com",
            "reg_email": "test1@gmail.com",
            "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
    }
}

我已经为这个类建模来解析这些数据

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

我得到了对我在下面创建的对象的响应。但是每当我尝试访问帐户信息时,我都会得到 ['UserAccount' 的实例]。我在哪里获得适当的用户价值。

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 test1@gmail.com
        print('this is userAccount : ${userDetails.accounts}'); // gets [Instance of UserAccount']
      }

我错过了什么吗?我还在学习flutter,任何建议都会有所帮助

【问题讨论】:

    标签: json flutter parsing


    【解决方案1】:

    账户信息以列表的形式。 您需要使用userDetails.accounts[index].id 或变量的任何名称来访问它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 2020-08-12
      • 2020-08-14
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多