将多级json字符串映射到java

时间:2017-09-01 03:30:49

标签: java android arrays json parsing

我有以下JSON结构:

  {
    "result": {
        "category": [{
                "id": "3",
                "name": "category name",
                "slug": "sllug",
                "image": "imageurl",
                "sub-categories": [{
                    "id": "3",
                    "name": "category name",
                    "slug": "sllug",
                    "image": "imageurl",
                    "sub-categories": [{
                        "id": "3",
                        "name": "category name",
                        "slug": "sllug",
                        "image": "imageurl",
                        "sub-categories": []
                    }]
                }]
            },

            {
                "id": "3",
                "name": "category name",
                "slug": "sllug",
                "image": "imageurl",
                "sub-categories": []
            }
        ]
    }
}

enter image description here

我需要使用上面的JSON创建类。

我创建了两个类HomeCategoryModelHomeSubCategoryModel

每个级别可能有多个子类别。

如何将这种类型的json映射到类中。

HomeCategoryModel 类:

    public class HomeCategoryModel {
    public int Id;
    public String Name;
    public String Slug;
    public String ImageUrl;
    public ArrayList<HomeSubCategoryModel> SubCategories;

    //...
    //getter, setter
}

HomeSubCategory 类:

public class HomeSubCategoryModel {
    public int Id;
    public String Name;
    public String Slug;
    public String ImageUrl;
    public ArrayList<HomeSubCategoryModel> SubCategories;

    //getter setter
}

我试图使用像这样的递归函数进行解析,但似乎无法工作:

    JSONObject allLists = jsonObject.getJSONObject("result");
    JSONArray catArray = allLists.getJSONArray("category");
    ArrayList<HomeCategoryModel> categoryList = new ArrayList<HomeCategoryModel>();
    for (int i = 0; i < catArray.length(); i++) {
        JSONObject jObj = catArray.getJSONObject(i);
        HomeCategoryModel categoryModel = new HomeCategoryModel();
        categoryModel.setId(Integer.parseInt(jObj.getString("id")));
        categoryModel.setName(jObj.getString("name"));
        categoryModel.setSlug(jObj.getString("slug"));
        categoryModel.setImageUrl(jObj.getString("image"));


        JSONArray productsArray = jObj.getJSONArray("sub-categories");
        if (productsArray.length() > 0) {
            parseSubCategories(productsArray);
        }

        categoryList.add(categoryModel);
    }

    public static ArrayList<HomeSubCategoryModel> parseSubCategories(JSONArray arr) {
    ArrayList<HomeSubCategoryModel> subLists = new ArrayList<HomeSubCategoryModel>();

    for (int i = 0; i < arr.length(); i++) {
        try {
            JSONObject childObj = arr.getJSONObject(i);
            HomeSubCategoryModel categoryModel = new HomeSubCategoryModel();
            categoryModel.setId(Integer.parseInt(childObj.getString("id")));
            categoryModel.setName(childObj.getString("name"));
            categoryModel.setSlug(childObj.getString("slug"));
            categoryModel.setImageUrl(childObj.getString("image"));
            JSONArray subArray = childObj.getJSONArray("sub-categories");
            if (subArray.length() > 0) {
                parseSubCategories(subArray);
            }
            subLists.add(categoryModel);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return subLists;

}

请建议我。谢谢。

1 个答案:

答案 0 :(得分:1)

如果您想使用Gson,那么我可以建议如下。 像这样生成你的POJO

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Example {

     @SerializedName("result")
     @Expose
     private Result result;

     public Result getResult() {
      return result;
     }

     public void setResult(Result result) {
      this.result = result;
     }

     public class Result {

      @SerializedName("category")
      @Expose
      private List < Category > category = null;

      public List < Category > getCategory() {
       return category;
      }

      public void setCategory(List < Category > category) {
       this.category = category;
      }

     }
     public class Category {

      @SerializedName("id")
      @Expose
      private String id;
      @SerializedName("name")
      @Expose
      private String name;
      @SerializedName("slug")
      @Expose
      private String slug;
      @SerializedName("image")
      @Expose
      private String image;
      @SerializedName("sub-categories")
      @Expose
      private List < SubCategory > subCategories = null;

      public String getId() {
       return id;
      }

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

      public String getName() {
       return name;
      }

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

      public String getSlug() {
       return slug;
      }

      public void setSlug(String slug) {
       this.slug = slug;
      }

      public String getImage() {
       return image;
      }

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

      public List < SubCategory > getSubCategories() {
       return subCategories;
      }

      public void setSubCategories(List < SubCategory > subCategories) {
       this.subCategories = subCategories;
      }
    }
      public class SubCategory_ {

       @SerializedName("id")
       @Expose
       private String id;
       @SerializedName("name")
       @Expose
       private String name;
       @SerializedName("slug")
       @Expose
       private String slug;
       @SerializedName("image")
       @Expose
       private String image;
       @SerializedName("sub-categories")
       @Expose
       private List < Object > subCategories = null;

       public String getId() {
        return id;
       }

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

       public String getName() {
        return name;
       }

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

       public String getSlug() {
        return slug;
       }

       public void setSlug(String slug) {
        this.slug = slug;
       }

       public String getImage() {
        return image;
       }

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

       public List < Object > getSubCategories() {
        return subCategories;
       }

       public void setSubCategories(List < Object > subCategories) {
        this.subCategories = subCategories;
       }

      }
}

然后

Gson gson = new Gson();
Example exp = gson.fromJson("your json string",Example.class);

你已经完成了。