How to Get Json object and Json Array in Same KEY by retrofit for android

Viewed 172

I am getting JSON Object when their is only one response(items) or JSON Array response but i want to array and object response.How to handle JSON Response?

JSON ARRAY when their is more than one items.And JSON OBJECT when their is only one items

.

{
   "product":[
     
      {
         "items":[
            {
               "name":"foo",
               "title":"toy"
            },
            {
               "name":"foo",
               "title":"toy"
            }
         ]
      },
     {   "items":
            {
               "name":"foo",
               "title":"toy"
            }
      }
      
      
   ]
}
1 Answers

First create model for this json Response using json to p

public class Example {

 @SerializedName("product")
 @Expose
private List<Product> product = null;

public List<Product> getProduct() {
return product;
}

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

}


  public class Item {

  @SerializedName("name")
  @Expose
  private String name;
  @SerializedName("title")
  @Expose
  private String title;

 public String getName() {
 return name;
}

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

 public String getTitle() {
 return title;
}

public void setTitle(String title) {
this.title = title;
}

}


public class Product {

 @SerializedName("items")
 @Expose
 private List<Item> items = null;

 public List<Item> getItems() {
 return items;
 }

public void setItems(List<Item> items) {
this.items = items;
}

}

Pass this Exmaple class into your response like Call and get the data usning model

Related