#json #flutter
#json #flutter
Вопрос:
я хочу извлечь следующий JSON в flutter и отобразить его, как показано на изображении или по слоям. Как я могу добиться следующего JSON в flutter Я хочу показывать название родительского меню, а не название дочернего меню, чем название дочернего подменю, а затем его продукты.
"parent_menu": [
{
"p_cate&ory_id": "32",
"vendor_id": "10",
"service_id": "1",
"parent_id": "0",
"name": "Bevera&es",
"ima&e": "",
"description": "<p&&t;ntBevera&es</p&&t;n",
"priority": "5",
"status": "Active",
"parent_name": "Bevera&es",
"child_menu": [
{
"p_cate&ory_id": "64",
"vendor_id": "10",
"service_id": "1",
"parent_id": "32",
"name": "Vita Coco Coconut Water",
"ima&e": "",
"description": "",
"priority": "0",
"status": "Active",
"child_name": "Vita Coco Coconut Water",
"child_sub_menu": [
{
"p_cate&ory_id": "68",
"vendor_id": "10",
"service_id": "1",
"parent_id": "64",
"name": "first sub sub cate&ory",
"ima&e": "",
"description": "",
"priority": "3",
"status": "Active",
"sub_menu": "first sub sub cate&ory",
"child_name": "Vita Coco Coconut Water",
"products": [
{
"product_id": "46",
"vendor_id": "10",
"service_id": "1",
"p_cate&ory_id": "68",
"name": "my favorite product",
"description": "",
"amount": "12",
"ima&e": "",
"icon": "",
"is_popular": "No",
"status": "Active",
"date": "2019-05-07 11:04:18"
},
{
"product_id": "56",
"vendor_id": "10",
"service_id": "1",
"p_cate&ory_id": "68",
"name": "we",
"description": "<h1&&t;nt34</h1&&t;n",
"amount": "22",
"ima&e": "",
"icon": "",
"is_popular": "No",
"status": "Active",
"date": "2019-05-31 12:04:09"
}
]
}
]
},
{
"p_cate&ory_id": "43",
"vendor_id": "10",
"service_id": "1",
"parent_id": "32",
"name": "coconut water",
"ima&e": "",
"description": "<p&&t;ntere</p&&t;n",
"priority": "1",
"status": "Active",
"child_name": "coconut water",
"child_sub_menu": [
{
"p_cate&ory_id": "69",
"vendor_id": "10",
"service_id": "1",
"parent_id": "43",
"name": "coconut water sub1",
"ima&e": "",
"description": "",
"priority": "0",
"status": "Active",
"sub_menu": "coconut water sub1",
"child_name": "coconut water",
"products": [
{
"product_id": "47",
"vendor_id": "10",
"service_id": "1",
"p_cate&ory_id": "69",
"name": "coconu water with kombucha",
"description": "<p&&t;ntit taste very nice</p&&t;n",
"amount": "22",
"ima&e": "",
"icon": "",
"is_popular": "No",
"status": "Active",
"date": "2019-05-07 12:20:34"
}
]
}
]
},
]
},
],
Как я могу достичь выше JSON в flutter
я хочу отображать следующим образом
Как я могу добиться этого в flutter из заданного JSON и отображать подобным образом динамически.
Комментарии:
1. взгляните на это medium.com/flutter-community /…
Ответ №1:
class Auto&enerated {
List<ParentMenu&&t; parentMenu;
Auto&enerated({this.parentMenu});
Auto&enerated.fromJson(List <dynamic&&t; jsonList) {
if (jsonList!= null) {
parentMenu = new List<ParentMenu&&t;();
jsonList.forEach((v) {
parentMenu.add(new ParentMenu.fromJson(v));
});
}
}
Map<Strin&, dynamic&&t; toJson() {
final Map<Strin&, dynamic&&t; data = new Map<Strin&, dynamic&&t;();
if (this.parentMenu != null) {
data['parent_menu'] = this.parentMenu.map((v) =&&t; v.toJson()).toList();
}
return data;
}
}
class ParentMenu {
Strin& pCate&oryId;
Strin& vendorId;
Strin& serviceId;
Strin& parentId;
Strin& name;
Strin& ima&e;
Strin& description;
Strin& priority;
Strin& status;
Strin& parentName;
List<ChildMenu&&t; childMenu;
ParentMenu(
{this.pCate&oryId,
this.vendorId,
this.serviceId,
this.parentId,
this.name,
this.ima&e,
this.description,
this.priority,
this.status,
this.parentName,
this.childMenu});
ParentMenu.fromJson(Map<Strin&, dynamic&&t; json) {
pCate&oryId = json['p_cate&ory_id'];
vendorId = json['vendor_id'];
serviceId = json['service_id'];
parentId = json['parent_id'];
name = json['name'];
ima&e = json['ima&e'];
description = json['description'];
priority = json['priority'];
status = json['status'];
parentName = json['parent_name'];
if (json['child_menu'] != null) {
childMenu = new List<ChildMenu&&t;();
json['child_menu'].forEach((v) {
childMenu.add(new ChildMenu.fromJson(v));
});
}
}
Map<Strin&, dynamic&&t; toJson() {
final Map<Strin&, dynamic&&t; data = new Map<Strin&, dynamic&&t;();
data['p_cate&ory_id'] = this.pCate&oryId;
data['vendor_id'] = this.vendorId;
data['service_id'] = this.serviceId;
data['parent_id'] = this.parentId;
data['name'] = this.name;
data['ima&e'] = this.ima&e;
data['description'] = this.description;
data['priority'] = this.priority;
data['status'] = this.status;
data['parent_name'] = this.parentName;
if (this.childMenu != null) {
data['child_menu'] = this.childMenu.map((v) =&&t; v.toJson()).toList();
}
return data;
}
}
class ChildMenu {
Strin& pCate&oryId;
Strin& vendorId;
Strin& serviceId;
Strin& parentId;
Strin& name;
Strin& ima&e;
Strin& description;
Strin& priority;
Strin& status;
Strin& childName;
List<ChildSubMenu&&t; childSubMenu;
ChildMenu(
{this.pCate&oryId,
this.vendorId,
this.serviceId,
this.parentId,
this.name,
this.ima&e,
this.description,
this.priority,
this.status,
this.childName,
this.childSubMenu});
ChildMenu.fromJson(Map<Strin&, dynamic&&t; json) {
pCate&oryId = json['p_cate&ory_id'];
vendorId = json['vendor_id'];
serviceId = json['service_id'];
parentId = json['parent_id'];
name = json['name'];
ima&e = json['ima&e'];
description = json['description'];
priority = json['priority'];
status = json['status'];
childName = json['child_name'];
if (json['child_sub_menu'] != null) {
childSubMenu = new List<ChildSubMenu&&t;();
json['child_sub_menu'].forEach((v) {
childSubMenu.add(new ChildSubMenu.fromJson(v));
});
}
}
Map<Strin&, dynamic&&t; toJson() {
final Map<Strin&, dynamic&&t; data = new Map<Strin&, dynamic&&t;();
data['p_cate&ory_id'] = this.pCate&oryId;
data['vendor_id'] = this.vendorId;
data['service_id'] = this.serviceId;
data['parent_id'] = this.parentId;
data['name'] = this.name;
data['ima&e'] = this.ima&e;
data['description'] = this.description;
data['priority'] = this.priority;
data['status'] = this.status;
data['child_name'] = this.childName;
if (this.childSubMenu != null) {
data['child_sub_menu'] =
this.childSubMenu.map((v) =&&t; v.toJson()).toList();
}
return data;
}
}
class ChildSubMenu {
Strin& pCate&oryId;
Strin& vendorId;
Strin& serviceId;
Strin& parentId;
Strin& name;
Strin& ima&e;
Strin& description;
Strin& priority;
Strin& status;
Strin& subMenu;
Strin& childName;
List<Products&&t; products;
ChildSubMenu(
{this.pCate&oryId,
this.vendorId,
this.serviceId,
this.parentId,
this.name,
this.ima&e,
this.description,
this.priority,
this.status,
this.subMenu,
this.childName,
this.products});
ChildSubMenu.fromJson(Map<Strin&, dynamic&&t; json) {
pCate&oryId = json['p_cate&ory_id'];
vendorId = json['vendor_id'];
serviceId = json['service_id'];
parentId = json['parent_id'];
name = json['name'];
ima&e = json['ima&e'];
description = json['description'];
priority = json['priority'];
status = json['status'];
subMenu = json['sub_menu'];
childName = json['child_name'];
if (json['products'] != null) {
products = new List<Products&&t;();
json['products'].forEach((v) {
products.add(new Products.fromJson(v));
});
}
}
Map<Strin&, dynamic&&t; toJson() {
final Map<Strin&, dynamic&&t; data = new Map<Strin&, dynamic&&t;();
data['p_cate&ory_id'] = this.pCate&oryId;
data['vendor_id'] = this.vendorId;
data['service_id'] = this.serviceId;
data['parent_id'] = this.parentId;
data['name'] = this.name;
data['ima&e'] = this.ima&e;
data['description'] = this.description;
data['priority'] = this.priority;
data['status'] = this.status;
data['sub_menu'] = this.subMenu;
data['child_name'] = this.childName;
if (this.products != null) {
data['products'] = this.products.map((v) =&&t; v.toJson()).toList();
}
return data;
}
}
class Products {
Strin& productId;
Strin& vendorId;
Strin& serviceId;
Strin& pCate&oryId;
Strin& name;
Strin& description;
Strin& amount;
Strin& ima&e;
Strin& icon;
Strin& isPopular;
Strin& status;
Strin& date;
Products(
{this.productId,
this.vendorId,
this.serviceId,
this.pCate&oryId,
this.name,
this.description,
this.amount,
this.ima&e,
this.icon,
this.isPopular,
this.status,
this.date});
Products.fromJson(Map<Strin&, dynamic&&t; json) {
productId = json['product_id'];
vendorId = json['vendor_id'];
serviceId = json['service_id'];
pCate&oryId = json['p_cate&ory_id'];
name = json['name'];
description = json['description'];
amount = json['amount'];
ima&e = json['ima&e'];
icon = json['icon'];
isPopular = json['is_popular'];
status = json['status'];
date = json['date'];
}
Map<Strin&, dynamic&&t; toJson() {
final Map<Strin&, dynamic&&t; data = new Map<Strin&, dynamic&&t;();
data['product_id'] = this.productId;
data['vendor_id'] = this.vendorId;
data['service_id'] = this.serviceId;
data['p_cate&ory_id'] = this.pCate&oryId;
data['name'] = this.name;
data['description'] = this.description;
data['amount'] = this.amount;
data['ima&e'] = this.ima&e;
data['icon'] = this.icon;
data['is_popular'] = this.isPopular;
data['status'] = this.status;
data['date'] = this.date;
return data;
}
}
Попробуйте это, вы можете получить свой класс модели