从flutter中的多个嵌套json数组中获取值

Posted

技术标签:

【中文标题】从flutter中的多个嵌套json数组中获取值【英文标题】:get value from multiple nested json array in flutter 【发布时间】:2020-11-29 20:10:50 【问题描述】:

我想在 Flutter 中获取以下 JSON 并将其显示为图像或分层显示。 我如何在颤动中实现以下 JSON 我想显示父菜单名称而不是子菜单名称而不是子菜单名称然后是其产品。

"parent_menu": [
            
                "p_category_id": "32",
                "vendor_id": "10",
                "service_id": "1",
                "parent_id": "0",
                "name": "Beverages",
                "image": "",
                "description": "<p>\n\tBeverages</p>\n",
                "priority": "5",
                "status": "Active",
                "parent_name": "Beverages",
                "child_menu": [
                    
                        "p_category_id": "64",
                        "vendor_id": "10",
                        "service_id": "1",
                        "parent_id": "32",
                        "name": "Vita Coco Coconut Water",
                        "image": "",
                        "description": "",
                        "priority": "0",
                        "status": "Active",
                        "child_name": "Vita Coco Coconut Water",
                        "child_sub_menu": [
                            
                                "p_category_id": "68",
                                "vendor_id": "10",
                                "service_id": "1",
                                "parent_id": "64",
                                "name": "first sub sub category",
                                "image": "",
                                "description": "",
                                "priority": "3",
                                "status": "Active",
                                "sub_menu": "first sub sub category",
                                "child_name": "Vita Coco Coconut Water",
                                "products": [
                                    
                                        "product_id": "46",
                                        "vendor_id": "10",
                                        "service_id": "1",
                                        "p_category_id": "68",
                                        "name": "my favorite product",
                                        "description": "",
                                        "amount": "12",
                                        "image": "",
                                        "icon": "",
                                        "is_popular": "No",
                                        "status": "Active",
                                        "date": "2019-05-07 11:04:18"
                                    ,
                                    
                                        "product_id": "56",
                                        "vendor_id": "10",
                                        "service_id": "1",
                                        "p_category_id": "68",
                                        "name": "we",
                                        "description": "<h1>\n\t34</h1>\n",
                                        "amount": "22",
                                        "image": "",
                                        "icon": "",
                                        "is_popular": "No",
                                        "status": "Active",
                                        "date": "2019-05-31 12:04:09"
                                    
                                ]
                            
                        ]
                    ,
                    
                        "p_category_id": "43",
                        "vendor_id": "10",
                        "service_id": "1",
                        "parent_id": "32",
                        "name": "coconut water",
                        "image": "",
                        "description": "<p>\n\tere</p>\n",
                        "priority": "1",
                        "status": "Active",
                        "child_name": "coconut water",
                        "child_sub_menu": [
                            
                                "p_category_id": "69",
                                "vendor_id": "10",
                                "service_id": "1",
                                "parent_id": "43",
                                "name": "coconut water sub1",
                                "image": "",
                                "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_category_id": "69",
                                        "name": "coconu water with kombucha",
                                        "description": "<p>\n\tit taste very nice</p>\n",
                                        "amount": "22",
                                        "image": "",
                                        "icon": "",
                                        "is_popular": "No",
                                        "status": "Active",
                                        "date": "2019-05-07 12:20:34"
                                    
                                ]
                            
                        ]
                    ,
                ]
            ,
        ],

如何在 Flutter 中实现上述 JSON 我想这样显示 我怎样才能从给定的 JSON 中实现这一点,并像这样动态显示。

【问题讨论】:

看看这个medium.com/flutter-community/… 【参考方案1】:
              class Autogenerated 
                List<ParentMenu> parentMenu;

                Autogenerated(this.parentMenu);

                Autogenerated.fromJson(List <dynamic> jsonList) 
                  if (jsonList!= null) 
                    parentMenu = new List<ParentMenu>();
                    jsonList.forEach((v) 
                      parentMenu.add(new ParentMenu.fromJson(v));
                    );
                  
                

                Map<String, dynamic> toJson() 
                  final Map<String, dynamic> data = new Map<String, dynamic>();
                  if (this.parentMenu != null) 
                    data['parent_menu'] = this.parentMenu.map((v) => v.toJson()).toList();
                  
                  return data;
                
              

              class ParentMenu 
                String pCategoryId;
                String vendorId;
                String serviceId;
                String parentId;
                String name;
                String image;
                String description;
                String priority;
                String status;
                String parentName;
                List<ChildMenu> childMenu;

                ParentMenu(
                    this.pCategoryId,
                    this.vendorId,
                    this.serviceId,
                    this.parentId,
                    this.name,
                    this.image,
                    this.description,
                    this.priority,
                    this.status,
                    this.parentName,
                    this.childMenu);

                ParentMenu.fromJson(Map<String, dynamic> json) 
                  pCategoryId = json['p_category_id'];
                  vendorId = json['vendor_id'];
                  serviceId = json['service_id'];
                  parentId = json['parent_id'];
                  name = json['name'];
                  image = json['image'];
                  description = json['description'];
                  priority = json['priority'];
                  status = json['status'];
                  parentName = json['parent_name'];
                  if (json['child_menu'] != null) 
                    childMenu = new List<ChildMenu>();
                    json['child_menu'].forEach((v) 
                      childMenu.add(new ChildMenu.fromJson(v));
                    );
                  
                

                Map<String, dynamic> toJson() 
                  final Map<String, dynamic> data = new Map<String, dynamic>();
                  data['p_category_id'] = this.pCategoryId;
                  data['vendor_id'] = this.vendorId;
                  data['service_id'] = this.serviceId;
                  data['parent_id'] = this.parentId;
                  data['name'] = this.name;
                  data['image'] = this.image;
                  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) => v.toJson()).toList();
                  
                  return data;
                
              

              class ChildMenu 
                String pCategoryId;
                String vendorId;
                String serviceId;
                String parentId;
                String name;
                String image;
                String description;
                String priority;
                String status;
                String childName;
                List<ChildSubMenu> childSubMenu;

                ChildMenu(
                    this.pCategoryId,
                    this.vendorId,
                    this.serviceId,
                    this.parentId,
                    this.name,
                    this.image,
                    this.description,
                    this.priority,
                    this.status,
                    this.childName,
                    this.childSubMenu);

                ChildMenu.fromJson(Map<String, dynamic> json) 
                  pCategoryId = json['p_category_id'];
                  vendorId = json['vendor_id'];
                  serviceId = json['service_id'];
                  parentId = json['parent_id'];
                  name = json['name'];
                  image = json['image'];
                  description = json['description'];
                  priority = json['priority'];
                  status = json['status'];
                  childName = json['child_name'];
                  if (json['child_sub_menu'] != null) 
                    childSubMenu = new List<ChildSubMenu>();
                    json['child_sub_menu'].forEach((v) 
                      childSubMenu.add(new ChildSubMenu.fromJson(v));
                    );
                  
                

                Map<String, dynamic> toJson() 
                  final Map<String, dynamic> data = new Map<String, dynamic>();
                  data['p_category_id'] = this.pCategoryId;
                  data['vendor_id'] = this.vendorId;
                  data['service_id'] = this.serviceId;
                  data['parent_id'] = this.parentId;
                  data['name'] = this.name;
                  data['image'] = this.image;
                  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) => v.toJson()).toList();
                  
                  return data;
                
              

              class ChildSubMenu 
                String pCategoryId;
                String vendorId;
                String serviceId;
                String parentId;
                String name;
                String image;
                String description;
                String priority;
                String status;
                String subMenu;
                String childName;
                List<Products> products;

                ChildSubMenu(
                    this.pCategoryId,
                    this.vendorId,
                    this.serviceId,
                    this.parentId,
                    this.name,
                    this.image,
                    this.description,
                    this.priority,
                    this.status,
                    this.subMenu,
                    this.childName,
                    this.products);

                ChildSubMenu.fromJson(Map<String, dynamic> json) 
                  pCategoryId = json['p_category_id'];
                  vendorId = json['vendor_id'];
                  serviceId = json['service_id'];
                  parentId = json['parent_id'];
                  name = json['name'];
                  image = json['image'];
                  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>();
                    json['products'].forEach((v) 
                      products.add(new Products.fromJson(v));
                    );
                  
                

                Map<String, dynamic> toJson() 
                  final Map<String, dynamic> data = new Map<String, dynamic>();
                  data['p_category_id'] = this.pCategoryId;
                  data['vendor_id'] = this.vendorId;
                  data['service_id'] = this.serviceId;
                  data['parent_id'] = this.parentId;
                  data['name'] = this.name;
                  data['image'] = this.image;
                  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) => v.toJson()).toList();
                  
                  return data;
                
              

              class Products 
                String productId;
                String vendorId;
                String serviceId;
                String pCategoryId;
                String name;
                String description;
                String amount;
                String image;
                String icon;
                String isPopular;
                String status;
                String date;

                Products(
                    this.productId,
                    this.vendorId,
                    this.serviceId,
                    this.pCategoryId,
                    this.name,
                    this.description,
                    this.amount,
                    this.image,
                    this.icon,
                    this.isPopular,
                    this.status,
                    this.date);

                Products.fromJson(Map<String, dynamic> json) 
                  productId = json['product_id'];
                  vendorId = json['vendor_id'];
                  serviceId = json['service_id'];
                  pCategoryId = json['p_category_id'];
                  name = json['name'];
                  description = json['description'];
                  amount = json['amount'];
                  image = json['image'];
                  icon = json['icon'];
                  isPopular = json['is_popular'];
                  status = json['status'];
                  date = json['date'];
                

                Map<String, dynamic> toJson() 
                  final Map<String, dynamic> data = new Map<String, dynamic>();
                  data['product_id'] = this.productId;
                  data['vendor_id'] = this.vendorId;
                  data['service_id'] = this.serviceId;
                  data['p_category_id'] = this.pCategoryId;
                  data['name'] = this.name;
                  data['description'] = this.description;
                  data['amount'] = this.amount;
                  data['image'] = this.image;
                  data['icon'] = this.icon;
                  data['is_popular'] = this.isPopular;
                  data['status'] = this.status;
                  data['date'] = this.date;
                  return data;
                
              

试试这个你可以得到你的模型类

【讨论】:

以上是关于从flutter中的多个嵌套json数组中获取值的主要内容,如果未能解决你的问题,请参考以下文章

flutter : 嵌套的 json 解析列表

从嵌套的 json 值对象中获取数组

Flutter 如何从 JSON 中列出具有多个子项的数组

无法从python中的json数组获取嵌套对象

在 Android 上通过 GSON 从嵌套 JSON 数组中获取价值? [复制]

从配置单元中的 JSON 数组获取 JSON 密钥时面临的问题