Home > Software engineering >  How we Deserialized this type of Json Array in Flutter?
How we Deserialized this type of Json Array in Flutter?

Time:05-03

This is Serialized json response

[
   {
      "AttendanceId":15,
      "UserId":1,
      "PunchInDateTime":"2022-04-30T05:24:20.337",
      "PunchInLatitude":23.03983,
      "PunchInLongitude":72.5602,
      "PunchOutDateTime":"2022-04-30T15:51:42.95",
      "PunchOutLatitude":23.039830,
      "PunchOutLongitude":72.560240,
      "AttendanceStatus":"P",
      "Remarks":"sample clockout",
      "IsActive":true,
      "CommandId":1,
      "CreateBy":0,
      "CreateDateTime":"2022-04-30T05:24:20.337",
      "UpdateBy":0,
      "UpdateDateTime":"2022-04-30T15:51:42.95"
   }
]

I want to Deserialized this response help on this i stuck in this task till 2 days

CodePudding user response:

I would create a class for this and Map the Data to a object https://docs.flutter.dev/development/data-and-backend/json

There are also websites that automatically convert the json response into a class. (https://javiercbk.github.io/json_to_dart/)

class TestClass {
  int? attendanceId;
  int? userId;
  String? punchInDateTime;
  double? punchInLatitude;
  double? punchInLongitude;
  String? punchOutDateTime;
  double? punchOutLatitude;
  double? punchOutLongitude;
  String? attendanceStatus;
  String? remarks;
  bool? isActive;
  int? commandId;
  int? createBy;
  String? createDateTime;
  int? updateBy;
  String? updateDateTime;

  TestClass(
      {this.attendanceId,
      this.userId,
      this.punchInDateTime,
      this.punchInLatitude,
      this.punchInLongitude,
      this.punchOutDateTime,
      this.punchOutLatitude,
      this.punchOutLongitude,
      this.attendanceStatus,
      this.remarks,
      this.isActive,
      this.commandId,
      this.createBy,
      this.createDateTime,
      this.updateBy,
      this.updateDateTime});

  TestClass.fromJson(Map<String, dynamic> json) {
    attendanceId = json['AttendanceId'];
    userId = json['UserId'];
    punchInDateTime = json['PunchInDateTime'];
    punchInLatitude = json['PunchInLatitude'];
    punchInLongitude = json['PunchInLongitude'];
    punchOutDateTime = json['PunchOutDateTime'];
    punchOutLatitude = json['PunchOutLatitude'];
    punchOutLongitude = json['PunchOutLongitude'];
    attendanceStatus = json['AttendanceStatus'];
    remarks = json['Remarks'];
    isActive = json['IsActive'];
    commandId = json['CommandId'];
    createBy = json['CreateBy'];
    createDateTime = json['CreateDateTime'];
    updateBy = json['UpdateBy'];
    updateDateTime = json['UpdateDateTime'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['AttendanceId'] = this.attendanceId;
    data['UserId'] = this.userId;
    data['PunchInDateTime'] = this.punchInDateTime;
    data['PunchInLatitude'] = this.punchInLatitude;
    data['PunchInLongitude'] = this.punchInLongitude;
    data['PunchOutDateTime'] = this.punchOutDateTime;
    data['PunchOutLatitude'] = this.punchOutLatitude;
    data['PunchOutLongitude'] = this.punchOutLongitude;
    data['AttendanceStatus'] = this.attendanceStatus;
    data['Remarks'] = this.remarks;
    data['IsActive'] = this.isActive;
    data['CommandId'] = this.commandId;
    data['CreateBy'] = this.createBy;
    data['CreateDateTime'] = this.createDateTime;
    data['UpdateBy'] = this.updateBy;
    data['UpdateDateTime'] = this.updateDateTime;
    return data;
  }
}

CodePudding user response:

You can use quicktype.io or json to dart to generate the class.

  • Related