2016-09-23 75 views
0

在以下json有兩個產品對象,產品不斷變化一些有超過100個產品(我試過jsonpojo轉換器,但它有兩個主產品內的產品)...產品是動態的...我使用改裝如何爲此JSON創建模型?

{ 
    "1": { 
    "entity_id": "11", 
    "type_id": "virtual", 
    "sku": "JIG0001VEN", 
    "name": "Test Chanakya", 
    "meta_title": "Test Chanakya", 
    "meta_description": "Test Chanakya", 
    "brand_name": "Chanakya", 
    "language": "English", 
    "no_test_attempts": "4", 
    "product_type": "25", 
    "difficulty_level": "20", 
    "jigno_certified": "18", 
    "ca_related": "23", 
    "description": "Test Chanakya", 
    "short_description": "Test Chanakya", 
    "meta_keyword": "Test Chanakya", 
    "regular_price_with_tax": 4500, 
    "regular_price_without_tax": 4500, 
    "final_price_with_tax": 4500, 
    "final_price_without_tax": 4500, 
    "is_saleable": true 
    }, 
    "2": { 
    "entity_id": "12", 
    "type_id": "virtual", 
    "sku": "JIG5555GENESIS", 
    "name": "Genesis Mentors GMAT Mock - 01", 
    "meta_title": "Genesis GMAT", 
    "brand_name": "Genesis Mentors", 
    "language": "English", 
    "meta_description": "Genesis GMAT", 
    "no_test_attempts": "3", 
    "product_type": "25", 
    "difficulty_level": "21", 
    "jigno_certified": "18", 
    "ca_related": "23", 
    "description": "Exhaustive national level ", 
    "meta_keyword": "GMAT, Genesis, Management", 
    "short_description": "Genesis Mentors GMAT Mock", 
    "regular_price_with_tax": 199, 
    "regular_price_without_tax": 199, 
    "final_price_with_tax": 199, 
    "final_price_without_tax": 199, 
    "is_saleable": true 
    } 
} 
+0

入住此http:// WWW。 jsonschema2pojo.org/ – Piyush

回答

1

有兩種方式:

第一種方法是使用GSON

請看看this library(Gson)

你可以找到在這個平臺上成千上萬的問題,您how to use Gson


第二種方式是採用了Android默認的庫

您只需產品對象的列表,所以你需要使用此庫並將其轉換爲模型後,可以使用JSONArray這個數組遍歷每個JSONObject

既然很簡單,我不會發布完整的對象。只需在對象中創建一個包含所有值的類並閱讀這些選項即可。

+0

是的,但上面的JSON產品1和2沒有密鑰,產品名稱不會總是1和2,它的動態 –

+0

@GowsikCristiano是的,但這仍然是一個數組,並且這些庫可以對它進行解碼,你不需要名稱 –

+0

我使用JSON TO POJO轉換器並得到了輸出,但是因爲每個產品都是一個對象...我創建了兩個名爲_1和_2的類...這對兩個產品來說很好,但是我有更多500個產品。 ..所以你可以建議任何型號 –

1

你可以通過這個鏈接來獲得它的詳細信息:: http://www.jsonschema2pojo.org/

OR

下面是解..

package com.example.models; 

public class ExampleResponse { 

DemoClassForOne _1; 
DemoClassForOne _2; 

public DemoClassForOne get_1() { 
    return _1; 
} 

public void set_1(DemoClassForOne _1) { 
    this._1 = _1; 
} 

public DemoClassForOne get_2() { 
    return _2; 
} 

public void set_2(DemoClassForOne _2) { 
    this._2 = _2; 
} 

public class DemoClassForOne{ 

    private String entityId; 
    private String typeId; 
    private String sku; 
    private String name; 
    private String metaTitle; 
    private String metaDescription; 
    private String brandName; 
    private String language; 
    private String noTestAttempts; 
    private String productType; 
    private String difficultyLevel; 
    private String jignoCertified; 
    private String caRelated; 
    private String description; 
    private String shortDescription; 
    private String metaKeyword; 
    private Integer regularPriceWithTax; 
    private Integer regularPriceWithoutTax; 
    private Integer finalPriceWithTax; 
    private Integer finalPriceWithoutTax; 
    private Boolean isSaleable; 


    /** 
    * 
    * @return 
    * The entityId 
    */ 
    public String getEntityId() { 
     return entityId; 
    } 

    /** 
    * 
    * @param entityId 
    * The entity_id 
    */ 
    public void setEntityId(String entityId) { 
     this.entityId = entityId; 
    } 

    /** 
    * 
    * @return 
    * The typeId 
    */ 
    public String getTypeId() { 
     return typeId; 
    } 

    /** 
    * 
    * @param typeId 
    * The type_id 
    */ 
    public void setTypeId(String typeId) { 
     this.typeId = typeId; 
    } 

    /** 
    * 
    * @return 
    * The sku 
    */ 
    public String getSku() { 
     return sku; 
    } 

    /** 
    * 
    * @param sku 
    * The sku 
    */ 
    public void setSku(String sku) { 
     this.sku = sku; 
    } 

    /** 
    * 
    * @return 
    * The name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * 
    * @param name 
    * The name 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * 
    * @return 
    * The metaTitle 
    */ 
    public String getMetaTitle() { 
     return metaTitle; 
    } 

    /** 
    * 
    * @param metaTitle 
    * The meta_title 
    */ 
    public void setMetaTitle(String metaTitle) { 
     this.metaTitle = metaTitle; 
    } 

    /** 
    * 
    * @return 
    * The metaDescription 
    */ 
    public String getMetaDescription() { 
     return metaDescription; 
    } 

    /** 
    * 
    * @param metaDescription 
    * The meta_description 
    */ 
    public void setMetaDescription(String metaDescription) { 
     this.metaDescription = metaDescription; 
    } 

    /** 
    * 
    * @return 
    * The brandName 
    */ 
    public String getBrandName() { 
     return brandName; 
    } 

    /** 
    * 
    * @param brandName 
    * The brand_name 
    */ 
    public void setBrandName(String brandName) { 
     this.brandName = brandName; 
    } 

    /** 
    * 
    * @return 
    * The language 
    */ 
    public String getLanguage() { 
     return language; 
    } 

    /** 
    * 
    * @param language 
    * The language 
    */ 
    public void setLanguage(String language) { 
     this.language = language; 
    } 

    /** 
    * 
    * @return 
    * The noTestAttempts 
    */ 
    public String getNoTestAttempts() { 
     return noTestAttempts; 
    } 

    /** 
    * 
    * @param noTestAttempts 
    * The no_test_attempts 
    */ 
    public void setNoTestAttempts(String noTestAttempts) { 
     this.noTestAttempts = noTestAttempts; 
    } 

    /** 
    * 
    * @return 
    * The productType 
    */ 
    public String getProductType() { 
     return productType; 
    } 

    /** 
    * 
    * @param productType 
    * The product_type 
    */ 
    public void setProductType(String productType) { 
     this.productType = productType; 
    } 

    /** 
    * 
    * @return 
    * The difficultyLevel 
    */ 
    public String getDifficultyLevel() { 
     return difficultyLevel; 
    } 

    /** 
    * 
    * @param difficultyLevel 
    * The difficulty_level 
    */ 
    public void setDifficultyLevel(String difficultyLevel) { 
     this.difficultyLevel = difficultyLevel; 
    } 

    /** 
    * 
    * @return 
    * The jignoCertified 
    */ 
    public String getJignoCertified() { 
     return jignoCertified; 
    } 

    /** 
    * 
    * @param jignoCertified 
    * The jigno_certified 
    */ 
    public void setJignoCertified(String jignoCertified) { 
     this.jignoCertified = jignoCertified; 
    } 

    /** 
    * 
    * @return 
    * The caRelated 
    */ 
    public String getCaRelated() { 
     return caRelated; 
    } 

    /** 
    * 
    * @param caRelated 
    * The ca_related 
    */ 
    public void setCaRelated(String caRelated) { 
     this.caRelated = caRelated; 
    } 

    /** 
    * 
    * @return 
    * The description 
    */ 
    public String getDescription() { 
     return description; 
    } 

    /** 
    * 
    * @param description 
    * The description 
    */ 
    public void setDescription(String description) { 
     this.description = description; 
    } 

    /** 
    * 
    * @return 
    * The shortDescription 
    */ 
    public String getShortDescription() { 
     return shortDescription; 
    } 

    /** 
    * 
    * @param shortDescription 
    * The short_description 
    */ 
    public void setShortDescription(String shortDescription) { 
     this.shortDescription = shortDescription; 
    } 

    /** 
    * 
    * @return 
    * The metaKeyword 
    */ 
    public String getMetaKeyword() { 
     return metaKeyword; 
    } 

    /** 
    * 
    * @param metaKeyword 
    * The meta_keyword 
    */ 
    public void setMetaKeyword(String metaKeyword) { 
     this.metaKeyword = metaKeyword; 
    } 

    /** 
    * 
    * @return 
    * The regularPriceWithTax 
    */ 
    public Integer getRegularPriceWithTax() { 
     return regularPriceWithTax; 
    } 

    /** 
    * 
    * @param regularPriceWithTax 
    * The regular_price_with_tax 
    */ 
    public void setRegularPriceWithTax(Integer regularPriceWithTax) { 
     this.regularPriceWithTax = regularPriceWithTax; 
    } 

    /** 
    * 
    * @return 
    * The regularPriceWithoutTax 
    */ 
    public Integer getRegularPriceWithoutTax() { 
     return regularPriceWithoutTax; 
    } 

    /** 
    * 
    * @param regularPriceWithoutTax 
    * The regular_price_without_tax 
    */ 
    public void setRegularPriceWithoutTax(Integer regularPriceWithoutTax) { 
     this.regularPriceWithoutTax = regularPriceWithoutTax; 
    } 

    /** 
    * 
    * @return 
    * The finalPriceWithTax 
    */ 
    public Integer getFinalPriceWithTax() { 
     return finalPriceWithTax; 
    } 

    /** 
    * 
    * @param finalPriceWithTax 
    * The final_price_with_tax 
    */ 
    public void setFinalPriceWithTax(Integer finalPriceWithTax) { 
     this.finalPriceWithTax = finalPriceWithTax; 
    } 

    /** 
    * 
    * @return 
    * The finalPriceWithoutTax 
    */ 
    public Integer getFinalPriceWithoutTax() { 
     return finalPriceWithoutTax; 
    } 

    /** 
    * 
    * @param finalPriceWithoutTax 
    * The final_price_without_tax 
    */ 
    public void setFinalPriceWithoutTax(Integer finalPriceWithoutTax) { 
     this.finalPriceWithoutTax = finalPriceWithoutTax; 
    } 

    /** 
    * 
    * @return 
    * The isSaleable 
    */ 
    public Boolean getIsSaleable() { 
     return isSaleable; 
    } 

    /** 
    * 
    * @param isSaleable 
    * The is_saleable 
    */ 
    public void setIsSaleable(Boolean isSaleable) { 
     this.isSaleable = isSaleable; 
    } 


} 

}

只是複製並粘貼&將其重命名.. 後,你可以訪問,使用得到&一套方法...... 欣賞,歡迎...

+0

嗨每個產品嗨...我試着上述代碼...如果產品是動態的...產品總數不斷變化 –