Fetching Nested JSON Arrays with retrofit?

I searched a lot in Google to find a way to use Nested Json Arrays with Retrofit, but it is useless Most of what I found talks about using JSON Objects with retrofit And my main issue is how to use the nested Array list inside call method to get data from the server I have this JSON

   {     "orders": [         {             "ID": "1",             "OrderDate": "2020-07-01",             "order_details": [                 {                     "detail_ID": "1",                     "description": "new order",                     "items": [                         {                             "item_ID": "1",                             "item_name": "milk",                             "Quantity": "4",                             "Price": "56.0",                             "UnitOfMeasure": "unit"                         },                         {                             "item_ID": "1",                             "item_name": "nuts",                             "Quantity": "6",                             "Price": "500",                             "UnitOfMeasure": "unit"                         }                     ]                 }             ]         }               ] } 

And those my other classes

Item.java

public class Item {  @SerializedName("item_ID") @Expose private String itemID; @SerializedName("item_name") @Expose private String itemName; @SerializedName("Quantity") @Expose private String quantity; @SerializedName("Price") @Expose private String price; @SerializedName("UnitOfMeasure") @Expose private String unit_Of_Measure;  public String getItemID() { return itemID; }  public void setItemID(String itemID) { this.itemID = itemID; }  public String getItemName() { return itemName; }  public void setItemName(String itemName) { this.itemName = itemName; }  public String getQuantity() { return quantity; }  public void setQuantity(String quantity) { this.quantity = quantity; }  public String getPrice() { return price; }  public void setPrice(String price) { this.price = price; }  public String getUnitOfMeasure() { return unitOfMeasure; }  public void setUnitOfMeasure(String unitOfMeasure) { this.unitOfMeasure = unitOfMeasure; }  } 

Order.java

public class Order {  @SerializedName("orders") @Expose private List<Order_> orders = null;  public List<Order_> getOrders() { return orders; }  public void setOrders(List<Order_> orders) { this.orders = orders; }  } 

OrderDetail.java

public class OrderDetail {  @SerializedName("detail_ID") @Expose private String detailID; @SerializedName("description") @Expose private String description; @SerializedName("items") @Expose private List<Item> items = null;  public String getDetailID() { return detailID; }  public void setDetailID(String detailID) { this.detailID = detailID; }  public String getDescription() { return description; }  public void setDescription(String description) { this.description = description; }  public List<Item> getItems() { return items; }  public void setItems(List<Item> items) { this.items = items; }  } 

Order_.java

public class Order_ {  @SerializedName("ID") @Expose private String iD; @SerializedName("OrderDate") @Expose private String orderDate; @SerializedName("order_details") @Expose private List<OrderDetail> orderDetails = null;  public String getID() { return iD; }  public void setID(String iD) { this.iD = iD; }  public String getOrderDate() { return orderDate; }  public void setOrderDate(String orderDate) { this.orderDate = orderDate; }  public List<OrderDetail> getOrderDetails() { return orderDetails; }  public void setOrderDetails(List<OrderDetail> orderDetails) { this.orderDetails = orderDetails; }  } 

ApiInterface.java

@GET("orders") Call<Order> getAllOrders(); 

Now I want to get order_detail, item data inside this function

  order.enqueue(new Callback<Order>() {             @Override             public void onResponse(Call<Order> call, Response<Order> response) {                 if(response.isSuccessful()){                  //here ..................                   }             }             @Override             public void onFailure(Call<TaskResponse> call, Throwable t) {                 Log.w(TAG, "onFailure: " + t.getMessage());             }         }); 

I will be so grateful who can help me

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.