I am not able to parse below mentioned json data in flutter.
[ { "id": 96, "name": "Entertainment", "link": "https://thehappilyeverafter.in/listing-category/entertainment/", "image": "https://thehappilyeverafter.in/wp-content/uploads/2021/05/a685b39b20592b03c0939878edb0cb84.jpg", "children": [ { "child_id": 114, "child_name": "DJs", "child_link": "https://thehappilyeverafter.in/listing-category/djs/" }, { "child_id": 117, "child_name": "Live Music", "child_link": "https://thehappilyeverafter.in/listing-category/live-music/" }, { "child_id": 115, "child_name": "Wedding Choreographer", "child_link": "https://thehappilyeverafter.in/listing-category/wedding-choreographer/" }, { "child_id": 116, "child_name": "Wedding Entertainment", "child_link": "https://thehappilyeverafter.in/listing-category/wedding-entertainment/" } ] }
]`
import 'dart:convert';
List<Root> rootFromJson(String str) => List<Root>.from(json.decode(str).map((x) => Root.fromJson(x)));
String rootToJson(List<Root> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Root {
Root({
this.id,
this.name,
this.link,
this.image,
this.children,
});
int id;
String name;
String link;
String image;
List<Children> children;
factory Root.fromJson(Map<String, dynamic> json) => Root(
id: json["id"],
name: json["name"],
link: json["link"],
image: json["image"] == null ? null : json["image"],
children: List<Children>.from(json["children"].map((x) => Children.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"link": link,
"image": image == null ? null : image,
"children": List<dynamic>.from(children.map((x) => x.toJson())),
};
}
class Children {
Children({
this.childId,
this.childName,
this.childLink,
});
int childId;
String childName;
String childLink;
factory Children.fromJson(Map<String, dynamic> json) => Children(
childId: json["child_id"],
childName: json["child_name"],
childLink: json["child_link"],
);
Map<String, dynamic> toJson() => {
"child_id": childId,
"child_name": childName,
"child_link": childLink,
};
}
[Getting this error][1] ` type 'List' is not a subtype of type 'Children'
Since your json is a list, you should loop through it:
final List<Root> roots = yourJson.map((element) {
return Root.fromJson(element);
}).toList();
print(roots.first.image);
//https://thehappilyeverafter.in/wp-content/uploads/2021/05/a685b39b20592b03c0939878edb0cb84.jpg
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.