I have a List that look like this:
"workinghours": [
{
"shop": 31,
"weekday": "monday",
"start_time": "05:25 AM",
"end_time": "11:00 AM",
"is_closed": false
},
{
"shop": 31,
"weekday": "monday",
"start_time": "01:10 PM",
"end_time": "05:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "tuesday",
"start_time": "12:00 PM",
"end_time": "05:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "tuesday",
"start_time": "05:00 PM",
"end_time": "10:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "tuesday",
"start_time": "04:00 AM",
"end_time": "05:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "wednesday",
"start_time": "03:00 AM",
"end_time": "11:00 AM",
"is_closed": false
},
{
"shop": 31,
"weekday": "wednesday",
"start_time": "01:00 PM",
"end_time": "11:00 AM",
"is_closed": false
},
{
"shop": 31,
"weekday": "wednesday",
"start_time": "07:00 PM",
"end_time": "11:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "thursday",
"start_time": "06:00 AM",
"end_time": "11:00 AM",
"is_closed": false
},
{
"shop": 31,
"weekday": "thursday",
"start_time": "01:00 PM",
"end_time": "05:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "thursday",
"start_time": "07:00 PM",
"end_time": "11:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "saturday",
"start_time": "05:00 AM",
"end_time": "12:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "saturday",
"start_time": "01:00 PM",
"end_time": "11:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "sunday",
"start_time": "05:00 AM",
"end_time": "11:00 PM",
"is_closed": false
},
{
"shop": 31,
"weekday": "sunday",
"start_time": "02:10 PM",
"end_time": "10:00 PM",
"is_closed": false
}
],
Its basically a list of weekdays and the time which the shop open and closes. I need to convert this list to the below json model:
{
"work_time": [
{
"shop_id": 11,
"week_day": "monday",
"work_time": [
{
"id": 11,
"startTime": "",
"endTime": "",
"isClosed": true
}
]
},
{
"shop_id": 12,
"week_day": "tuesday",
"work_time": [
{
"id": 12,
"startTime": "",
"endTime": "",
"isClosed": true
},
{
"id": 12,
"startTime": "",
"endTime": "",
"isClosed": true
}
]
}
]
}
And the model class is
ShopWorkTimeModel shopWorkTimeModelFromJson(String str) => ShopWorkTimeModel.fromJson(json.decode(str));
String shopWorkTimeModelToJson(ShopWorkTimeModel data) => json.encode(data.toJson());
class ShopWorkTimeModel {
ShopWorkTimeModel({
this.workTime,
});
List<ShopWorkTimeModelWorkTime> workTime;
factory ShopWorkTimeModel.fromJson(Map<String, dynamic> json) => ShopWorkTimeModel(
workTime: List<ShopWorkTimeModelWorkTime>.from(json["work_time"].map((x) => ShopWorkTimeModelWorkTime.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"work_time": List<dynamic>.from(workTime.map((x) => x.toJson())),
};
}
class ShopWorkTimeModelWorkTime {
ShopWorkTimeModelWorkTime({
this.workingHourId,
this.weekDay,
this.workTime,
});
int workingHourId;
String weekDay;
List<WorkTimeWorkTime> workTime;
factory ShopWorkTimeModelWorkTime.fromJson(Map<String, dynamic> json) => ShopWorkTimeModelWorkTime(
workingHourId: json["working_hour_id"],
weekDay: json["week_day"],
workTime: List<WorkTimeWorkTime>.from(json["work_time"].map((x) => WorkTimeWorkTime.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"working_hour_id": workingHourId,
"week_day": weekDay,
"work_time": List<dynamic>.from(workTime.map((x) => x.toJson())),
};
}
class WorkTimeWorkTime {
WorkTimeWorkTime({
this.id,
this.startTime,
this.endTime,
this.isClosed,
});
int id;
String startTime;
String endTime;
bool isClosed;
factory WorkTimeWorkTime.fromJson(Map<String, dynamic> json) => WorkTimeWorkTime(
id: json["id"],
startTime: json["startTime"],
endTime: json["endTime"],
isClosed: json["isClosed"],
);
Map<String, dynamic> toJson() => {
"id": id,
"startTime": startTime,
"endTime": endTime,
"isClosed": isClosed,
};
}
How to parse the above json to this model class in dart. In the above json file the list contains weekdays more than one times, so i need to convert that by having one weekday and list all the working hours in a list below that weekdays as i mentioned in the above model class.