2017-09-01 57 views
-2

我有以下JSON結構:地圖多層次的JSON字符串類對象在Java中

{ 
    "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; 

} 

請給我建議。謝謝。

+0

什麼是在控制檯上的錯誤? –

+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); 

和你做。

+0

這是我正在尋找。這是完美的。非常感謝。投票並標記爲答案。我用這個來獲得最後一個子類別。 'example.result.category.get(0).subCategories.get(0).subCategories.get(0)' 還有一個問題。如何從'LinkedTreeMap'獲取鍵值。 –

相關問題