简体   繁体   中英

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

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

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

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