简体   繁体   中英

How to create a Model for this JSON?

In the following json there are two product objects, the products keep changing some times there are more than 100 products (I tried json to pojo converter but it has two for two products inside the main class) ... the products are dynamic ... I am using retrofit

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

There are two ways:

First method is using Gson

Please have a look at this library(Gson) .

You can find on this platform thousands of questions about how to use Gson


Second way is using Android default libraries

You simply have a list of products objects, so you need to use a JSONArray after you can iterate each JSONObject inside this array using this library and converting it into a model.

Since it's easy i'm not going to post the complete object. simply create a class with all values in the object and read about those options.

You can go through this link to get it in Details :: http://www.jsonschema2pojo.org/

OR

Here is the solution..

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;
    }


}

}

Just Copy and Paste & rename it.. After that you can access that using get & set method... Appreciation are welcome...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM