I tried to convert json below to dart class with many online converters also. But when i implemented that code dose not work and give me some error.
json:
{
"sections": [
{
"id": "863",
"title": "الفصل الاول (1)",
"course_id": 110011,
"description": "",
"order": "2",
"percent": 16.666666666666664,
"items": [
{
"id": 110167,
"type": "lp_quiz",
"title": "الإختبار",
"preview": false,
"duration": "01 hours",
"graduation": "failed",
"status": "completed",
"locked": false
}
]
}
]
}
CodePudding user response:
you can use this page https://javiercbk.github.io/json_to_dart/
just paste your json, it will create your model
CodePudding user response:
You can covert it using this website , I use it always and it works fine This is you json code after been converted to a dart class :
import 'dart:convert';
ModelClass modelClassFromJson(String str) => ModelClass.fromJson(json.decode(str));
String modelClassToJson(ModelClass data) => json.encode(data.toJson());
class ModelClass {
ModelClass({
this.sections,
});
List<Section> sections;
factory ModelClass.fromJson(Map<String, dynamic> json) => ModelClass(
sections: List<Section>.from(json["sections"].map((x) => Section.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"sections": List<dynamic>.from(sections.map((x) => x.toJson())),
};
}
class Section {
Section({
this.id,
this.title,
this.courseId,
this.description,
this.order,
this.percent,
this.items,
});
String id;
String title;
int courseId;
String description;
String order;
double percent;
List<Item> items;
factory Section.fromJson(Map<String, dynamic> json) => Section(
id: json["id"],
title: json["title"],
courseId: json["course_id"],
description: json["description"],
order: json["order"],
percent: json["percent"].toDouble(),
items: List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"course_id": courseId,
"description": description,
"order": order,
"percent": percent,
"items": List<dynamic>.from(items.map((x) => x.toJson())),
};
}
class Item {
Item({
this.id,
this.type,
this.title,
this.preview,
this.duration,
this.graduation,
this.status,
this.locked,
});
int id;
String type;
String title;
bool preview;
String duration;
String graduation;
String status;
bool locked;
factory Item.fromJson(Map<String, dynamic> json) => Item(
id: json["id"],
type: json["type"],
title: json["title"],
preview: json["preview"],
duration: json["duration"],
graduation: json["graduation"],
status: json["status"],
locked: json["locked"],
);
Map<String, dynamic> toJson() => {
"id": id,
"type": type,
"title": title,
"preview": preview,
"duration": duration,
"graduation": graduation,
"status": status,
"locked": locked,
};
}