在Retrofit2中如何解析动态内部内长数组响应

时间:2019-07-08 07:33:04

标签: android json gson retrofit2

在通过json解析retrofit2响应时,我沉迷于一个奇怪的问题。实际上,我有一个父类别,它具有不同的子类别,这些子类别可以变化(动态)而不是固定的。一个父类别有2个子类别,其他父类别有5个或可能是1个子类别。因此,这方面的反应正在到来。

我正在使用翻新和gson库来获取json响应。

下面是一些我已经使用过的代码段

public class CategoryDetails {

    @SerializedName("icon")
    @Expose
    private String icon;
    @SerializedName("childs")
    @Expose
    private List<ChildCategoryDetail> childList;
    @SerializedName("categories_id")
    @Expose
    private String id;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("categories_name")
    @Expose
    private String name;
    @SerializedName("parent_id")
    @Expose
    private String parentId;
    @SerializedName("total_products")
    @Expose
    private String totalProducts;

    public String getTotalProducts() {
        return totalProducts;
    }

    public void setTotalProducts(String totalProducts) {
        this.totalProducts = totalProducts;
    }

    public List<ChildCategoryDetail> getChildList() {
        return childList;
    }

    public void setChildList(List<ChildCategoryDetail> childList) {
        this.childList = childList;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }


    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }


}

及其子类别

public class ChildCategoryDetail {


    @SerializedName("icon")
    @Expose
    private String icon;
    @SerializedName("categories_id")
    @Expose
    private String id;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("categories_name")
    @Expose
    private String name;
    @SerializedName("parent_id")
    @Expose
    private String parentId;
    @SerializedName("total_products")
    @Expose
    private String totalProducts;

    public String getTotalProducts() {
        return totalProducts;
    }

    public void setTotalProducts(String totalProducts) {
        this.totalProducts = totalProducts;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }


    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }



}

以及下面我在这里使用它的逻辑

// Get CategoriesList from AppContext
        allCategoriesList = ((App) getContext().getApplicationContext()).getCategoriesList();

        int size = allCategoriesList.size();
        allSubCategoriesList = new ArrayList<>();

        // Get SubCategoriesList from AllCategoriesList
        for (int i=0;  i<allCategoriesList.size();  i++) {
            int sizeStr = allCategoriesList.get(i).getChildList().size();
            for (int j=0;j<allCategoriesList.get(i).getChildList().size();j++) {
                allSubCategoriesList.add(allCategoriesList.get(i).getChildList().get(j));
            }

        }

根据以上逻辑,我只能进入子类别的第一级,而不是内部第二或第三级,依此类推...

那么json是

{
    "success": "1",
    "data": [
        {
            "categories_id": 3,
            "categories_name": "Boy's Clothing",
            "parent_id": 0,
            "image": "images/media/2019/07/yvWJL03110.png",
            "icon": "images/media/2019/07/yvWJL03110.png",
            "vendors_id": 3,
            "total_products": 16,
            "childs": [
                {
                    "categories_id": 22,
                    "categories_name": "Boy Polo shirts",
                    "parent_id": 3,
                    "image": "images/media/2019/07/yvWJL03110.png",
                    "icon": "images/media/2019/07/yvWJL03110.png",
                    "vendors_id": 4,
                    "total_products": 4,
                    "childs": [
                        {
                            "categories_id": 48,
                            "categories_name": "Boy Polo Shirts sub-1",
                            "parent_id": 22,
                            "image": "images/media/2019/07/yvWJL03110.png",
                            "icon": "images/media/2019/07/fyprD03510.png",
                            "vendors_id": 5,
                            "total_products": 0
                        },
                        {
                            "categories_id": 49,
                            "categories_name": "Boy Polo Shirts sub-2",
                            "parent_id": 22,
                            "image": "images/media/2019/07/yvWJL03110.png",
                            "icon": "images/media/2019/07/fyprD03510.png",
                            "vendors_id": 5,
                            "total_products": 0
                        }
                    ]
                },
                {
                    "categories_id": 23,
                    "categories_name": "Boy Casual Shirts",
                    "parent_id": 3,
                    "image": "images/media/2019/07/yvWJL03110.png",
                    "icon": "images/media/2019/07/yvWJL03110.png",
                    "vendors_id": 4,
                    "total_products": 5
                },
                {
                    "categories_id": 24,
                    "categories_name": "Boy Pants & Jeans",
                    "parent_id": 3,
                    "image": "images/media/2019/07/yvWJL03110.png",
                    "icon": "images/media/2019/07/yvWJL03110.png",
                    "vendors_id": 4,
                    "total_products": 4
                },
                {
                    "categories_id": 25,
                    "categories_name": "Boy Shoes",
                    "parent_id": 3,
                    "image": "images/media/2019/07/yvWJL03110.png",
                    "icon": "images/media/2019/07/yvWJL03110.png",
                    "vendors_id": 4,
                    "total_products": 3
                }
            ]
        },
        {
            "categories_id": 4,
            "categories_name": "Girl's Clothing",
            "parent_id": 0,
            "image": "images/media/2019/07/yvWJL03110.png",
            "icon": "images/media/2019/07/yvWJL03110.png",
            "vendors_id": 3,
            "total_products": 6,
            "childs": [
                {
                    "categories_id": 26,
                    "categories_name": "Dresses & Rompers",
                    "parent_id": 4,
                    "image": "images/media/2019/07/yvWJL03110.png",
                    "icon": "images/media/2019/07/yvWJL03110.png",
                    "vendors_id": 4,
                    "total_products": 3
                },
                {
                    "categories_id": 27,
                    "categories_name": "Shorts & Skirts",
                    "parent_id": 4,
                    "image": "images/media/2019/07/yvWJL03110.png",
                    "icon": "images/media/2019/07/yvWJL03110.png",
                    "vendors_id": 4,
                    "total_products": 3
                },
                {
                    "categories_id": 28,
                    "categories_name": "Sweaters",
                    "parent_id": 4,
                    "image": "images/media/2019/07/yvWJL03110.png",
                    "icon": "images/media/2019/07/yvWJL03110.png",
                    "vendors_id": 4,
                    "total_products": 0
                }
            ]
        }
    ],
    "message": "Returned all categories.",
    "categories": 2
}

我想进入所有内部层次,我也希望产品针对与category_id关联的那个类别

1 个答案:

答案 0 :(得分:0)

尝试使用此伪代码

void main() {

    ArrayList<Object> mainChilds = new ArrayList<Object>();

    /* Replace Object with your pojo */
    ArrayList<Object> allSubCategoriesList = new ArrayList<>();

    allSubCategoriesList.addAll(getSubChilds(mainChilds));
}

ArrayList<Object> getSubChilds(ArrayList<Object> childs){
    /* Replace Object with your pojo */
    ArrayList<Object> subChilds = new ArrayList<>();

    // Get SubCategoriesList from AllCategoriesList
    for (int i = 0; i < childs.size(); i++) {
        //// add the Child to the list is you want
        subChilds.add(childs.get(i));

        /// check the is there are subChilds exists or not
        if(childs.get(i).getSubChilds().size > 0) {
            ///// pass those subchilds to function
            subChilds.addAll(getSubChilds(childs.get(i).getSubChilds()));
        }
    }

    return subChilds;
}