2016-09-23 5 views
0

다음은 json입니다. 제품에는 두 가지 제품 개체가 있습니다. 제품은 100 개 이상의 제품이 있습니다. (~ pojo 변환기를 사용해 보았지만 주 제품 내부에는 2 개의 제품이 2 개 있습니다.) 제품이 동적입니다이 JSON에 대한 모델을 만드는 방법은 무엇입니까?

가에서 참조하시기 바랍니다

첫 번째 방법은 GSON을 사용하고 있습니다 : ... 나는 두 가지 방법이 있습니다 개조

{ 
    "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 GET 위해 JSON을 복사 할 수 있습니다 : // www가 있습니다. jsonschema2pojo.org/ – Piyush

답변

1

을 사용하고 있습니다. 당신이 필요하므로,

당신은 how to use Gson


두 번째 방법에 대해 수많은 질문이 플랫폼에서 찾을 수 있습니다 당신은 단순히 제품의 개체의 목록을 안드로이드 기본 라이브러리

을 사용하고 있습니다 이 라이브러리를 사용하여이 배열 안에있는 각 JSONObject을 반복하여 모델로 변환 한 후에 JSONArray을 사용하십시오.

완전한 개체를 게시하지 않으므로 쉽습니다. 단순히 개체의 모든 값을 가진 클래스를 만들고 그 옵션에 대해 읽으십시오. 당신은이 링크를 통해 갈 수

+0

그렇습니다. 위의 JSON 제품 1과 2에는 키가 없으며 제품 이름은 항상 1과 2가 아닙니다. 동적 인 –

+0

@ GowsikCristiano 예.하지만 여전히 배열이며 해당 라이브러리는이를 디코딩 할 수 있습니다. 당신은 이름이 필요하지 않습니다 –

+0

JSON TO POJO 변환기를 사용하여 출력물을 얻었지만 각 제품이 객체이기 때문에 ... _1과 _2라는 이름의 두 클래스를 만들었습니다 ... 두 제품에는 문제가 없지만 그 이상은 가지고 있습니다. 500 제품. .. 그래서 어떤 모델을 제안 할 수 있습니다 –

1

는 세부 사항에 들어 가지 :: 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; 
    } 


} 

}

그냥 복사 붙여 넣기 & 이름 바꾸기 .. 그 후 당신은 ... 얻을 & set 메소드를 사용하여 ... 감사 환영 것을이 링크를

+0

안녕하세요 ... 위의 코드를 시도 ... 제품이 동적 인 경우 어떻게해야합니까? ... 제품의 총 수가 계속 변화하고 있습니다. –