1

how to parse this result array with gson?. I stuck for two days i can not parse. From SO and few other places, I have found that I need to define a top level container but I do not know how to complete its definition.

results:[
{
  "SupplierCatalog": {
    "supplier_catalog_id": "139",
    "distributor_id": "57",
    "distributor_asking_price": "999.99",
    "supplier_id": null,
    "product_name": "jjjjjjjj j j j j j j  jj j jjjjjjjjjjjj",
    "product_description": "kkkkkkkkkkkkkk k k  k k"
  },
  "image_details": {
    "isCustomImageProvided": 0,
    "isImageUploadedTo": 1,

  }
},
{
  "SupplierCatalog": {
    "supplier_catalog_id": "138",
    "distributor_id": "57",
    "distributor_asking_price": "69.25",
    "supplier_id": null,
    "product_name": "i+am+editing+this",
    "product_description": "using+wait+for+it..........+the+API"
  },
  "image_details": {
    "isCustomImageProvided": 1,
    "isImageUploadedTo": 1,

  }
},
{
  "SupplierCatalog": {
    "supplier_catalog_id": "137",
    "distributor_id": "57",
    "distributor_asking_price": "69.69",
    "supplier_id": null,
    "product_name": "Supplier Created Product Sample",
    "product_description": "This is a sample description"
  },
  "image_details": {
    "isCustomImageProvided": 1,
    "isImageUploadedTo": 1,

  }
},
{
  "SupplierCatalog": {
    "supplier_catalog_id": "136",
    "distributor_id": "57",
    "distributor_asking_price": "45.58",
    "supplier_id": null,
    "product_name": "Distributor Created Product Sample",
    "product_description": "Blah Blah Blah"
  },
  "image_details": {
    "isCustomImageProvided": 1,
    "isImageUploadedTo": 1,

  }
},
{
  "SupplierCatalog": {
    "supplier_catalog_id": "135",
    "distributor_id": "57",
    "distributor_asking_price": "99.99",
    "supplier_id": null,
    "product_name": "Distributor Created Product Sample",
    "product_description": "In publishing and graphic design, lorem ipsum is a filler text commonly used to demonstrate the graphic elements of a document or visual presentation. Replacing meaningful content that could be distracting with placeholder text may allow viewers to focus on graphic aspects such as font, typography, and page layout.\r\n\r\nThe lorem ipsum text is typically a scrambled section of De finibus bonorum et malorum, a 1st-century BC Latin text by Cicero, with words altered, added, and removed such that it is nonsensical, improper Latin.\r\n\r\nA variation of the ordinary lorem ipsum text has been used in typesetting since the 1960s or earlier, when it was popularized by advertisements for Letraset transfer sheets. It was introduced to the Information Age in the mid-1980s by Aldus Corporation, which employed it in graphics and word processing templates for its desktop publishing program, PageMaker, for the Apple Macintosh.[1]"
  },
  "image_details": {
    "isCustomImageProvided": 1,
    "isImageUploadedTo": 1,

  }
  }
]

I can not parse with making two class. so please help. And after that how i get individual get method. Thanks.

1
  • what have you done so far? Commented Jul 29, 2014 at 14:52

1 Answer 1

1

Here is an example of what the structure might look like, depending on your json results it might change slightly.

package com.example.test;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String json = "{results:[{\"SupplierCatalog\":{\"supplier_catalog_id\": \"139\",\"distributor_id\": \"57\",\"distributor_asking_price\": \"999.99\",\"supplier_id\": null,\"product_name\": \"jjjjjjjj j j j j j j  jj j jjjjjjjjjjjj\",\"product_description\": \"kkkkkkkkkkkkkk k k  k k\"},\"image_details\": {\"isCustomImageProvided\": 0,\"isImageUploadedTo\": 1}}]}";

        JsonParseResult jsonParseResult = new Gson().fromJson(json, JsonParseResult.class);

        if (jsonParseResult != null && jsonParseResult.getResult() != null) {
            for (Result result : jsonParseResult.getResult()) {
                Log.d(TAG, "Result: " + result.toString());
            }
        }
    }

    public class JsonParseResult {

        @SerializedName("results")
        private List<Result> results;

        public JsonParseResult(List<Result> results) {
            super();
            this.results = results;
        }

        public List<Result> getResult() {
            return results;
        }

        public void setResult(List<Result> results) {
            this.results = results;
        }

    }

    public class Result {

        @SerializedName("SupplierCatalog")
        private SupplierCatalog supplierCatalog;

        @SerializedName("image_details")
        private ImageDetails imageDetails;

        public Result(SupplierCatalog supplierCatalog, ImageDetails imageDetails) {
            super();
            this.supplierCatalog = supplierCatalog;
            this.imageDetails = imageDetails;
        }

        public SupplierCatalog getSupplierCatalog() {
            return supplierCatalog;
        }

        public void setSupplierCatalog(SupplierCatalog supplierCatalog) {
            this.supplierCatalog = supplierCatalog;
        }

        public ImageDetails getImageDetails() {
            return imageDetails;
        }

        public void setImageDetails(ImageDetails imageDetails) {
            this.imageDetails = imageDetails;
        }

        @Override
        public String toString() {
            return "Result [supplierCatalog=" + supplierCatalog + ", imageDetails=" + imageDetails + "]";
        }

    }

    public class SupplierCatalog {

        @SerializedName("supplier_catalog_id")
        private Integer supplierCatalogId;

        @SerializedName("distributor_id")
        private Integer distributorId;

        @SerializedName("distributor_asking_price")
        private Double distributorAskingPrice;

        @SerializedName("supplier_id")
        private Integer supplierId;

        @SerializedName("product_name")
        private String productName;

        @SerializedName("product_description")
        private String productDescription;

        public SupplierCatalog(Integer supplierCatalogId, Integer distributorId, Double distributorAskingPrice, Integer supplierId, String productName, String productDescription) {
            super();
            this.supplierCatalogId = supplierCatalogId;
            this.distributorId = distributorId;
            this.distributorAskingPrice = distributorAskingPrice;
            this.supplierId = supplierId;
            this.productName = productName;
            this.productDescription = productDescription;
        }

        public Integer getSupplierCatalogId() {
            return supplierCatalogId;
        }

        public void setSupplierCatalogId(Integer supplierCatalogId) {
            this.supplierCatalogId = supplierCatalogId;
        }

        public Integer getDistributorId() {
            return distributorId;
        }

        public void setDistributorId(Integer distributorId) {
            this.distributorId = distributorId;
        }

        public Double getDistributorAskingPrice() {
            return distributorAskingPrice;
        }

        public void setDistributorAskingPrice(Double distributorAskingPrice) {
            this.distributorAskingPrice = distributorAskingPrice;
        }

        public Integer getSupplierId() {
            return supplierId;
        }

        public void setSupplierId(Integer supplierId) {
            this.supplierId = supplierId;
        }

        public String getProductName() {
            return productName;
        }

        public void setProductName(String productName) {
            this.productName = productName;
        }

        public String getProductDescription() {
            return productDescription;
        }

        public void setProductDescription(String productDescription) {
            this.productDescription = productDescription;
        }

        @Override
        public String toString() {
            return "SupplierCatalog [supplierCatalogId=" + supplierCatalogId + ", distributorId=" + distributorId + ", distributorAskingPrice=" + distributorAskingPrice + ", supplierId=" + supplierId + ", productName=" + productName + ", productDescription=" + productDescription + "]";
        }

    }

    public class ImageDetails {

        @SerializedName("isCustomImageProvided")
        private Integer isCustomImageProvided;

        @SerializedName("isImageUploadedTo")
        private Integer isImageUploadedTo;

        public ImageDetails(Integer isCustomImageProvided, Integer isImageUploadedTo) {
            super();
            this.isCustomImageProvided = isCustomImageProvided;
            this.isImageUploadedTo = isImageUploadedTo;
        }

        public Integer getIsCustomImageProvided() {
            return isCustomImageProvided;
        }

        public void setIsCustomImageProvided(Integer isCustomImageProvided) {
            this.isCustomImageProvided = isCustomImageProvided;
        }

        public Integer getIsImageUploadedTo() {
            return isImageUploadedTo;
        }

        public void setIsImageUploadedTo(Integer isImageUploadedTo) {
            this.isImageUploadedTo = isImageUploadedTo;
        }

        @Override
        public String toString() {
            return "ImageDetails [isCustomImageProvided=" + isCustomImageProvided + ", isImageUploadedTo=" + isImageUploadedTo + "]";
        }

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks willie for your answer.i have done some changes according to my response and it works. thanks
My pleasure. Glad you were able to sort it out. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.