[英]get value from nested json array and check if it is empty or not in flutter
My json array looks like:我的 json 数组如下所示:
[
{
"sub_categories": [],
"category_id": "82",
"catgory_name": "Andrew Murray 1 Month",
"parent_cat_id": "1"
},
{
"sub_categories": [
{
"category_id": "177",
"catgory_name": "2 Samuel",
"parent_cat_id": "167"
}
],
"category_id": "167",
"catgory_name": "The Bible ASV",
"parent_cat_id": "1"
},
]
First i want to display "catgory_name" in listview and if that catgory_name has sub_categories array than i need to display it in another list , so how can i achieve this.首先,我想在列表视图中显示“catgory_name”,如果该 catgory_name 具有 sub_categories 数组而不是我需要在另一个列表中显示它,那么我该如何实现这一点。 i get all catgory_name by following code:我通过以下代码获取所有 catgory_name:
class CategoryModel {
final String name;
final List<SubCategoryModel> SubCategory;
CategoryModel({
this.name,
this.SubCategory,
});
factory CategoryModel.fromJson(Map<String, dynamic> json) {
return new CategoryModel(
name: json['catgory_name'].toString(),
SubCategory: parsesub_categories(json['sub_categories']),
// SubCategory:(json['sub_categories'] as List).map((map) => map).toList(),
);
}
static List<SubCategoryModel> parsesub_categories(cateJson) {
List<SubCategoryModel> catlist = new List<SubCategoryModel>.from(cateJson);
return catlist;
}
but sub_categories i could not get that array .但是 sub_categories 我无法获得该数组。
You can create data model as below:您可以创建如下数据模型:
class CategoryModel {
List<SubCateogryModel> subCategories;
String categoryId;
String catgoryName;
String parentCatId;
CategoryModel(
{this.subCategories,
this.categoryId,
this.catgoryName,
this.parentCatId});
CategoryModel.fromJson(Map<String, dynamic> json) {
if (json['sub_categories'] != null) {
subCategories = new List<SubCateogryModel>();
json['sub_categories'].forEach((v) {
subCategories.add(new SubCateogryModel.fromJson(v));
});
}
categoryId = json['category_id'];
catgoryName = json['catgory_name'];
parentCatId = json['parent_cat_id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.subCategories != null) {
data['sub_categories'] =
this.subCategories.map((v) => v.toJson()).toList();
}
data['category_id'] = this.categoryId;
data['catgory_name'] = this.catgoryName;
data['parent_cat_id'] = this.parentCatId;
return data;
}
}
class SubCateogryModel {
String categoryId;
String catgoryName;
String parentCatId;
SubCateogryModel({this.categoryId, this.catgoryName, this.parentCatId});
SubCateogryModel.fromJson(Map<String, dynamic> json) {
categoryId = json['category_id'];
catgoryName = json['catgory_name'];
parentCatId = json['parent_cat_id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['category_id'] = this.categoryId;
data['catgory_name'] = this.catgoryName;
data['parent_cat_id'] = this.parentCatId;
return data;
}
}
Now, you have to parse your json array into Data model array现在,您必须将 json 数组解析为数据模型数组
List<CategoryModel> categoryList = [];
jsonArray.forEach((val){
categoryList.add(CategoryModel.fromJson(val));
});
Now, the UI code,现在,UI 代码,
ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: Text(categoryList[index].catgoryName),
subtitle: categoryList[index].subCategories.isNotEmpty
? Column(
children: List.generate(
categoryList[index].subCategories.length, (position) {
String subCategory = categoryList[index]
.subCategories[position]
.catgoryName;
return Text(subCategory);
}),
)
: SizedBox(),
);
},
itemCount: categoryList.length,
)
You can use QuickType.io to generate dart classes (PODOs) for json.您可以使用QuickType.io为 json 生成 dart 类 (PODO)。
import 'dart:convert';
List<CategoryModel> categoryModelFromJson(String str) => List<CategoryModel>.from(json.decode(str).map((x) => CategoryModel.fromJson(x)));
String categoryModelToJson(List<CategoryModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class CategoryModel {
List<CategoryModel> subCategories;
String categoryId;
String catgoryName;
String parentCatId;
CategoryModel({
this.subCategories,
this.categoryId,
this.catgoryName,
this.parentCatId,
});
factory CategoryModel.fromJson(Map<String, dynamic> json) => CategoryModel(
subCategories: json["sub_categories"] == null ? null : List<CategoryModel>.from(json["sub_categories"].map((x) => CategoryModel.fromJson(x))),
categoryId: json["category_id"],
catgoryName: json["catgory_name"],
parentCatId: json["parent_cat_id"],
);
Map<String, dynamic> toJson() => {
"sub_categories": subCategories == null ? null : List<dynamic>.from(subCategories.map((x) => x.toJson())),
"category_id": categoryId,
"catgory_name": catgoryName,
"parent_cat_id": parentCatId,
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.