[英]How to create a Model for this JSON?
在下面的json
,有兩個產品對象,產品有時會不斷變化,有100多個產品(我嘗試了json
到pojo
轉換器,但在主類中有兩個產品有兩個)...這些產品是動態的..我正在使用改造
{
"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
}
}
有兩種方法:
第一種方法是使用Gson
請看一下這個庫(Gson) 。
您可以在該平台上找到有關如何使用Gson的數千個問題
第二種方法是使用Android默認庫
您僅具有產品對象列表,因此在使用此庫可以迭代此數組內的每個JSONObject並將其轉換為模型之后,需要使用JSONArray 。
既然很容易,我就不會發布完整的對象。 只需創建一個包含對象中所有值的類,然后了解這些選項。
您可以通過此鏈接在詳細信息中獲取它:: http://www.jsonschema2pojo.org/
要么
這是解決方案。
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;
}
}
}
只需復制並粘貼並重命名它即可。之后,您可以使用get&set方法訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.