json - 列表中的列表JSON解析在 flutter

标签 json string flutter dart

这是我要解析的json,其中包含vendorCategory的列表,其中包含子类别的更多列表,但我遇到了错误

 {
 "vendorcategory": [
        {
            "id": "ctg-1",
            "name": "Fruits and Vegetables",
            "childcategories":[
                {
                    "id": "ctg-1A",
                    "name": "Fresh Vegetables",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1A-1",
                            "name": "Potato, Onion, Tomato",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-2",
                            "name": "Leafy Vegetables",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-3",
                            "name": "Root Vegetables",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-4",
                            "name": "Cucumber & Capsicum",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-4",
                            "name": "Cabbage & Cauliflower",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-5",
                            "name": "Beans, Brinjals & Okra",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-6",
                            "name": "Gourd, Pumpkin, Drumstick",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-7",
                            "name": "Specialty",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1B",
                    "name": "Herbs & Seasonings",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1B-1",
                            "name": "Lemon, Ginger & Garlic",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1B-2",
                            "name": "Indian & Exotic Herbs",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1C",
                    "name": "Fresh Fruits",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1C-1",
                            "name": "Mangoes",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-2",
                            "name": "Bananna, Sopota & Papaya",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-3",
                            "name": "Apples & Pomegranate",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-4",
                            "name": "Kivi, Melon, Citrus fruit",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-5",
                            "name": "Seasonal Fruits",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-6",
                            "name": "Fruit Baskets",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1D",
                    "name": "Exotic Fruits & Veggies",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1D-1",
                            "name": "Exotic Fruits",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1D-2",
                            "name": "Exotic Vegetables",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1E",
                    "name": "Organic Fruits & Vegetables",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1E-1",
                            "name": "Organic Vegetables",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1E-2",
                            "name": "Organic Fruits",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1F",
                    "name": "Cuts & Sprouts",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1F-1",
                            "name": "Cut & Peeled Veggies",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1F-2",
                            "name": "Cut Fruits, Tender Coconut",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1F-3",
                            "name": "Fresh Salads & Sprouts",
                            "iKey": "Blobkey"
                        }
                    ]
                }
            ]
        },
        {
            "id": "ctg-2",
            "name": "Foodgrains, Oil And Massala",
            "childcategories": {
                "id": "ctg-2",
                "name": "Foodgrains, Oil And Massala",
                "childcategories": [
                    {
                        "id": "ctg-2A",
                        "name": "Flours",
                        "childcategories": [
                            {
                                "id": "ctg-2A-1",
                                "name": "Whole Wheat"
                            },
                            {
                                "id": "ctg-2A-2",
                                "name": "Sooji/Maida"
                            },
                            {
                                "id": "ctg-2A-3",
                                "name": "Other Flours"
                            }
                        ]
                    },
                    {
                        "id": "ctg-2B",
                        "name": "Dal and Pulses"
                    },
                    {
                        "id": "ctg-2C",
                        "name": "Rice and Rice Products"
                    },
                    {
                        "id": "ctg-2D",
                        "name": "Oil and Ghee"
                    }
                ]
            }
        },
        {
            "id": "ctg-3",
            "name": "Bread, Bakery and Dairy",
            "childcategories": [
                {
                    "id": "ctg-3A",
                    "name": "Dairy",
                    "childcategories": [
                        {
                            "id": "ctg-3A-1",
                            "name": "Milk"
                        },
                        {
                            "id": "ctg-3A-2",
                            "name": "Butter/Cheese"
                        },
                        {
                            "id": "ctg-3A-3",
                            "name": "Curd"
                        },
                        {
                            "id": "ctg-3A-4",
                            "name": "Milk Powders"
                        }
                    ]
                }
            ]
        }
    ]
 }
我创建了将json转换为dart对象的模型
     class Vendorcategory {
   String id;
    String name;
    List<Vendorcategory> childCategoris;
   Vendorcategory({this.id, this.name, this.childCategoris});
     factory Vendorcategory.fromJson(Map<String, dynamic> jsonMap) {
      return Vendorcategory(
        id: jsonMap['id'],
        name: jsonMap['name'],
      childCategoris: jsonMap["childcategories"],
    );}}
我收到发生异常的错误。
_TypeError(类型“列表”不是类型“列表“Vendorcategory””的子类型),
我的完整json是这个
   {"vendor": {
    "id": "vd-2bd9a914-481f-4057-99a9-14481f505709",
    "_cO": 1598431685512,
   
    "vendorcategory": [
        {
            "id": "ctg-1",
            "name": "Fruits and Vegetables",
            "childcategories":[
                {
                    "id": "ctg-1A",
                    "name": "Fresh Vegetables",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1A-1",
                            "name": "Potato, Onion, Tomato",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-2",
                            "name": "Leafy Vegetables",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-3",
                            "name": "Root Vegetables",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-4",
                            "name": "Cucumber & Capsicum",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-4",
                            "name": "Cabbage & Cauliflower",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-5",
                            "name": "Beans, Brinjals & Okra",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-6",
                            "name": "Gourd, Pumpkin, Drumstick",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1A-7",
                            "name": "Specialty",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1B",
                    "name": "Herbs & Seasonings",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1B-1",
                            "name": "Lemon, Ginger & Garlic",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1B-2",
                            "name": "Indian & Exotic Herbs",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1C",
                    "name": "Fresh Fruits",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1C-1",
                            "name": "Mangoes",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-2",
                            "name": "Bananna, Sopota & Papaya",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-3",
                            "name": "Apples & Pomegranate",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-4",
                            "name": "Kivi, Melon, Citrus fruit",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-5",
                            "name": "Seasonal Fruits",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1C-6",
                            "name": "Fruit Baskets",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1D",
                    "name": "Exotic Fruits & Veggies",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1D-1",
                            "name": "Exotic Fruits",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1D-2",
                            "name": "Exotic Vegetables",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1E",
                    "name": "Organic Fruits & Vegetables",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1E-1",
                            "name": "Organic Vegetables",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1E-2",
                            "name": "Organic Fruits",
                            "iKey": "Blobkey"
                        }
                    ]
                },
                {
                    "id": "ctg-1F",
                    "name": "Cuts & Sprouts",
                    "iKey": "Blobkey",
                    "childcategories": [
                        {
                            "id": "ctg-1F-1",
                            "name": "Cut & Peeled Veggies",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1F-2",
                            "name": "Cut Fruits, Tender Coconut",
                            "iKey": "Blobkey"
                        },
                        {
                            "id": "ctg-1F-3",
                            "name": "Fresh Salads & Sprouts",
                            "iKey": "Blobkey"
                        }
                    ]
                }
            ]
        },
        {
            "id": "ctg-2",
            "name": "Foodgrains, Oil And Massala",
            "childcategories": {
                "id": "ctg-2",
                "name": "Foodgrains, Oil And Massala",
                "childcategories": [
                    {
                        "id": "ctg-2A",
                        "name": "Flours",
                        "childcategories": [
                            {
                                "id": "ctg-2A-1",
                                "name": "Whole Wheat"
                            },
                            {
                                "id": "ctg-2A-2",
                                "name": "Sooji/Maida"
                            },
                            {
                                "id": "ctg-2A-3",
                                "name": "Other Flours"
                            }
                        ]
                    },
                    {
                        "id": "ctg-2B",
                        "name": "Dal and Pulses"
                    },
                    {
                        "id": "ctg-2C",
                        "name": "Rice and Rice Products"
                    },
                    {
                        "id": "ctg-2D",
                        "name": "Oil and Ghee"
                    }
                ]
            }
        },
        {
            "id": "ctg-3",
            "name": "Bread, Bakery and Dairy",
            "childcategories": [
                {
                    "id": "ctg-3A",
                    "name": "Dairy",
                    "childcategories": [
                        {
                            "id": "ctg-3A-1",
                            "name": "Milk"
                        },
                        {
                            "id": "ctg-3A-2",
                            "name": "Butter/Cheese"
                        },
                        {
                            "id": "ctg-3A-3",
                            "name": "Curd"
                        },
                        {
                            "id": "ctg-3A-4",
                            "name": "Milk Powders"
                     }]}]}]}}
这是我创建的vendorItem类
    class VendorItem {
   String id;
  int iCO;
  Md mMd;
   List<Admins> admins;
  List<Vendorcategory> vendorcategory;
   List<Images> lImages;
  String name;
  String orgName;
  String registeredAddress;

  String country;
   String currency;
   Status status;
  Status paymentOption;
  Status pickup;
  Status viewOption;
   VendorAccount vendorAccount;

      VendorItem(
  {this.id,
  this.iCO,
  this.mMd,
  this.admins,
  this.vendorcategory,
  this.lImages,
  this.name,
  this.orgName,
  this.registeredAddress,
  this.country,
  this.currency,
  this.status,
  this.paymentOption,
  this.pickup,
  this.viewOption,
  this.vendorAccount});

    VendorItem.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    iCO = json['_cO'];
   mMd = json['_md'] != null ? Md.fromJson(json['_md']) : null;
   if (json['admins'] != null) {
  admins = List<Admins>();
  json['admins'].forEach((v) {
    admins.add(Admins.fromJson(v));
  });
   }
  if (json['vendorcategory'] != null) {
   vendorcategory = List<Vendorcategory>();
  json['vendorcategory'].forEach((v) {
    vendorcategory.add(Vendorcategory.fromJson(v));
  });
}
if (json['_images'] != null) {
  lImages = List<Images>();
  json['_images'].forEach((v) {
    lImages.add(Images.fromJson(v));
  });
}
name = json['name'];
orgName = json['orgName'];
registeredAddress = json['registeredAddress'];

   country = json['country'];
    currency = json['currency'];
    status = json['status'] != null ? Status.fromJson(json['status']) : 
  null;
paymentOption = json['paymentOption'] != null
    ? Status.fromJson(json['paymentOption'])
      : null;
   pickup = json['pickup'] != null ? Status.fromJson(json['pickup']) : 
   null;
  viewOption =
    json['viewOption'] != null ? Status.fromJson(json['viewOption']) : 
     null;
      vendorAccount = json['vendorAccount'] != null
        ? VendorAccount.fromJson(json['vendorAccount'])
    : null;
  }
  }

最佳答案

该错误是因为List<Vendorcategory>没有在Vendorcategory中退出。
正确的方法应该是这样的

Response response = await _service.yourApi();    
var res = VendorcategoryResponse.fromJson(response.body);
供应商类别响应
part 'vendor_category_response.g.dart';

@JsonSerializable()
class VendorcategoryResponse {
  var vendorcategory = List<dynamic>();
  VendorcategoryResponse();

  factory VendorcategoryResponse.fromJson(Map<String, dynamic> json) =>
      _$VendorcategoryResponseFromJson(json);
  Map<String, dynamic> toJson() => _$VendorcategoryResponseToJson(this);
}

关于json - 列表中的列表JSON解析在 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64412531/

相关文章:

java - 将 JSON 解析为 java 类

vb.net - 在 Visual Basic 中何时使用 CDbl 和 CStr 函数

java - 替换字符串中的字符,Java

Flutter 布局问题 : stack z-order not working

java - 如何访问 JSONObject、JSONObject 和 JSONArray 中的内容? Java/安卓

javascript - 从 WEB 拉取 JSON 时出现内存泄漏

c# - 使用JSON.net解析json字符串

c - 当我向后浏览字符串时,为什么会在字符串开头遇到 NULL 终止字符?

linux - Flutter 医生搜索位置

flutter - android 后退按钮不会调用 WillPopScope 的 onWillPop