json - 列表中的列表 json 在颤动中解析
问题描述
这是我要解析的 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(类型'List'不是类型'List“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);
}
推荐阅读
- mysql - MySQL:计算列中的宽度日期
- javascript - NodeJs Express - 如何返回 HTTP 错误代码 (400,401,500) 和 JSON 对象
- javascript - jQuery 读取 .txt 文件但获取特定数据
- python - 使用 PyMC3 运行贝叶斯伽马回归的难度
- swift - 使用 Alamofire 随机获取 NSURLErrorDomain Code = -1206
- node.js - webpack 错误:自定义关键字定义无效:数据/错误应该是布尔值
- angular - 对象复制问题
- .net - 如何让我的单元测试绕过我的 .NET Web 控制器上的 Authorize 标记
- php - 主题的 wp-options 中的 Wordpress 阿拉伯语类别名称问题
- c# - 将多行数据导出到csv中的同一单元格