本文介绍了从嵌套的json数组中获取值,并检查它是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的json数组如下:

My json array looks like:

[
    {
        "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"
    },
]

首先,我想在列表视图中显示"catgory_name",如果该catgory_name具有sub_categories数组,而不是我需要在另一个列表中显示它,那么我该如何实现这一点.我通过以下代码获得所有的catgory_name:

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.i get all catgory_name by following code:

 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;
  }

但是sub_categories我无法获得该数组.

but sub_categories i could not get that array .

推荐答案

您可以按以下方式创建数据模型:

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;
  }
}

现在,您必须将json数组解析为数据模型数组

Now, you have to parse your json array into Data model array

  List<CategoryModel> categoryList = [];

  jsonArray.forEach((val){
      categoryList.add(CategoryModel.fromJson(val));
    });

现在,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,
      )

这篇关于从嵌套的json数组中获取值,并检查它是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:33
查看更多