asiya
asiya

Reputation: 280

how to parse this jsonarray using GSON library

This is my jsonarray.

[
  {
    "id": 3,
    "title": "Best Seller",
    "promotedProducts": [
      {
        "product": {
          "id": 4208,
          "name": "Gents T-Shirt With Navy Blue Collar cuff",
          "reviewList": [],
          "productDetail": {
            "id": 4207,
            "length": 33,
            "breadth": 27
          },
          "attributeList": [
            {
              "id": 1,
              "productId": 4208
            }
          ]
        }
      },
      {
        "product": {
          "id": 4208,
          "name": "Gents T-Shirt With Navy Blue Collar cuff",
          "reviewList": [],
          "productDetail": {
            "id": 4207,
            "length": 33,
            "breadth": 27
          },
          "attributeList": [
            {
              "id": 1,
              "productId": 4208
            }
          ]
        }
      }
    ]
  }
]

I had created Homecollection class for it.also added following code for parsing.I had also created subclasses for product,promotedProducts,productDetail,attributeList,images.its give me as reponse of two items but other details are coming empty

 Gson gson = new Gson();
                HomeProducts homeProducts = HomeProducts.getInstance();
                List<HomeCollections> collectionList = new ArrayList<HomeCollections>();
              collectionList = Arrays.asList(gson.fromJson(response.toString(), HomeCollections[].class));

Upvotes: 1

Views: 380

Answers (3)

Madhav Gor
Madhav Gor

Reputation: 211

    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    @Generated("org.jsonschema2pojo")
    public class AttributeList {

        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("productId")
        @Expose
        private Integer productId;

        /**
         *
         * @return
         * The id
         */
        public Integer getId() {
            return id;
        }

        /**
         *
         * @param id
         * The id
         */
        public void setId(Integer id) {
            this.id = id;
        }

        /**
         *
         * @return
         * The productId
         */
        public Integer getProductId() {
            return productId;
        }

        /**
         *
         * @param productId
         * The productId
         */
        public void setProductId(Integer productId) {
            this.productId = productId;
        }

    }
    -----------------------------------com.example.Example.java-----------------------------------

    package com.example;

    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    @Generated("org.jsonschema2pojo")
    public class Example {

        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("title")
        @Expose
        private String title;
        @SerializedName("promotedProducts")
        @Expose
        private List<PromotedProduct> promotedProducts = new ArrayList<PromotedProduct>();

        /**
         *
         * @return
         * The id
         */
        public Integer getId() {
            return id;
        }

        /**
         *
         * @param id
         * The id
         */
        public void setId(Integer id) {
            this.id = id;
        }

        /**
         *
         * @return
         * The title
         */
        public String getTitle() {
            return title;
        }

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

        /**
         *
         * @return
         * The promotedProducts
         */
        public List<PromotedProduct> getPromotedProducts() {
            return promotedProducts;
        }

        /**
         *
         * @param promotedProducts
         * The promotedProducts
         */
        public void setPromotedProducts(List<PromotedProduct> promotedProducts) {
            this.promotedProducts = promotedProducts;
        }

    }
    -----------------------------------com.example.Product.java-----------------------------------

    package com.example;

    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    @Generated("org.jsonschema2pojo")
    public class Product {

        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("reviewList")
        @Expose
        private List<Object> reviewList = new ArrayList<Object>();

        private ProductDetail productDetail;

        private List<AttributeList> attributeList = new ArrayList<AttributeList>();

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Object> getReviewList() {
            return reviewList;
        }

        public void setReviewList(List<Object> reviewList) {
            this.reviewList = reviewList;
        }

        public ProductDetail getProductDetail() {
            return productDetail;
        }

        public void setProductDetail(ProductDetail productDetail) {
            this.productDetail = productDetail;
        }


        public List<AttributeList> getAttributeList() {
            return attributeList;
        }

        public void setAttributeList(List<AttributeList> attributeList) {
            this.attributeList = attributeList;
        }

    }
    -----------------------------------com.example.ProductDetail.java-----------------------------------

    package com.example;

    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;


    public class ProductDetail {

        private Integer id;

        private Integer length;

        private Integer breadth;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getLength() {
            return length;
        }

        public void setLength(Integer length) {
            this.length = length;
        }

        public Integer getBreadth() {
            return breadth;
        }

        public void setBreadth(Integer breadth) {
            this.breadth = breadth;
        }

    }
    -----------------------------------com.example.PromotedProduct.java-----------------------------------

    package com.example;

    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class PromotedProduct {

        private Product product;

        public Product getProduct() {
            return product;
        }

        public void setProduct(Product product) {
            this.product = product;
        }

    }

use this classes to get this data

Upvotes: 1

Andrei T
Andrei T

Reputation: 3083

You can use the following website to parse:

jsonschema2pojo

A version would be:

public class PromotedProduct {

@SerializedName("product")
@Expose
private Product product;

/**
 * 
 * @return
 * The product
 */
 public Product getProduct() {
   return product;
 }

 /**
  * 
  * @param product
  * The product
  */
  public void setProduct(Product product) {
   this.product = product;
 }
}

ProductDetail:
public class ProductDetail {

   @SerializedName("id")
   @Expose
   private Integer id;
   @SerializedName("length")
   @Expose
   private Integer length;
   @SerializedName("breadth")
   @Expose
   private Integer breadth;

  /** 
   *  
   * @return
   * The id
   */
   public Integer getId() {
      return id;
   }

   /**
    * 
   * @param id
   * The id
   */
   public void setId(Integer id) {
      this.id = id;
   }

   /**
    * 
    * @return
    * The length
    */
    public Integer getLength() {
      return length;
    }

    /**
     *  
     * @param length
     * The length
     */
    public void setLength(Integer length) {
       this.length = length;
    }

  /**
   * 
   * @return
   * The breadth
   */
   public Integer getBreadth() {
     return breadth;
   }

  /**
   * 
   * @param breadth
   * The breadth
   */
   public void setBreadth(Integer breadth) {
      this.breadth = breadth;
   }
}

Attributes:

 public class AttributeList {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("productId")
@Expose
private Integer productId;

/**
* 
* @return
* The id
*/
public Integer getId() {
return id;
}

/**
* 
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}

/**
* 
* @return
* The productId
*/
public Integer getProductId() {
return productId;
}

/**
* 
* @param productId
* The productId
*/
public void setProductId(Integer productId) {
this.productId = productId;
}

}


The main class:

public class Example {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("promotedProducts")
@Expose
private List<PromotedProduct> promotedProducts = new ArrayList<PromotedProduct>();

/**
* 
* @return
* The id
*/
public Integer getId() {
return id;
}

/**
* 
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}

/**
* 
* @return
* The title
*/
public String getTitle() {
return title;
}

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

/**
* 
* @return
* The promotedProducts
*/
public List<PromotedProduct> getPromotedProducts() {
return promotedProducts;
}

/**
* 
* @param promotedProducts
* The promotedProducts
*/
public void setPromotedProducts(List<PromotedProduct> promotedProducts) {
this.promotedProducts = promotedProducts;
}

}

And the last Product:

public class Product {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("reviewList")
@Expose
private List<Object> reviewList = new ArrayList<Object>();
@SerializedName("productDetail")
@Expose
private ProductDetail productDetail;
@SerializedName("attributeList")
@Expose
private List<AttributeList> attributeList = new ArrayList<AttributeList>();

/**
* 
* @return
* The id
*/
public Integer getId() {
return id;
}

/**
* 
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}

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

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

/**
* 
* @return
* The reviewList
*/
public List<Object> getReviewList() {
return reviewList;
}

/**
* 
* @param reviewList
* The reviewList
*/
public void setReviewList(List<Object> reviewList) {
this.reviewList = reviewList;
}

/**
* 
* @return
* The productDetail
*/
public ProductDetail getProductDetail() {
return productDetail;
}

/**
* 
* @param productDetail
* The productDetail
*/
public void setProductDetail(ProductDetail productDetail) {
this.productDetail = productDetail;
}

/**
* 
* @return
* The attributeList
*/
public List<AttributeList> getAttributeList() {
return attributeList;
}

/**
* 
* @param attributeList
* The attributeList
*/
public void setAttributeList(List<AttributeList> attributeList) {
this.attributeList = attributeList;
}

}


Of course you need to polish it a bit. I just copy-pasted the generated files. You can change your main class there from "Example" to whatever you may like plus remove some stuff you do not like.

Upvotes: 0

Sunil Sunny
Sunil Sunny

Reputation: 3994

Create a Model class like this.

public class ProductDetail
    {
        public int id { get; set; }
        public int length { get; set; }
        public int breadth { get; set; }
    }

public class AttributeList
{
    public int id { get; set; }
    public int productId { get; set; }
}

public class Product
{
    public int id { get; set; }
    public string name { get; set; }
    public List<object> reviewList { get; set; }
    public ProductDetail productDetail { get; set; }
    public List<AttributeList> attributeList { get; set; }
}

public class PromotedProduct
{
    public Product product { get; set; }
}

public class HomeCollections
{
    public int id { get; set; }
    public string title { get; set; }
    public List<PromotedProduct> promotedProducts { get; set; }
}

Now use the GSON like this. Here url is the source link.You will get the response as a model. Now

response.promotedProducts will give you all list of items.

    InputStream source = retrieveStream(url);
    Gson gson = new Gson();
    Reader reader = new InputStreamReader(source);
    HomeCollections response = gson.fromJson(reader, HomeCollections.class);

Upvotes: 2

Related Questions