i\'m new to retrofit,below is the json
{
\"response\": \"success\",
\"servicecode\": \"134\",
\"forecast\": {
\"month\": {
\"jan\": [
{
method 1 - without using model class
Suppose your "month" object is jsonObjectResponse
.You can make use of Iterator
JSONObject jsonResponse = new JSONObject(jsonObjectResponse);
Iterator iteratorObj = jsonResponse.keys();
while (iteratorObj.hasNext())
{
JSONArray monthArray=(JSONArray)iteratorObj.next()
//add all monthArray to an arraylist
}
** Edit for model class **
method 2 - using model class
You can use Map<String, List<MonthModel>>
to get dynamic response from month
You have two create three model clases like this:
Example.java
public class Example {
@SerializedName("response")
@Expose
private String response;
@SerializedName("servicecode")
@Expose
private String servicecode;
@SerializedName("forecast")
@Expose
private Forecast forecast;
@SerializedName("message")
@Expose
private String message;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public String getServicecode() {
return servicecode;
}
public void setServicecode(String servicecode) {
this.servicecode = servicecode;
}
public Forecast getForecast() {
return forecast;
}
public void setForecast(Forecast forecast) {
this.forecast = forecast;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Forecast.java
public class Forecast {
@SerializedName("month")
@Expose
private Map<String, List<MonthModel>> result;
public Map<String, List<MonthModel>> getResult() {
return result;
}
public void setResult(Map<String, List<MonthModel>> result) {
this.result = result;
}
}
MonthModel.java
public class MonthModel {
@SerializedName("id")
@Expose
private String id;
@SerializedName("price")
@Expose
private String price;
@SerializedName("Product")
@Expose
private String product;
@SerializedName("Qty")
@Expose
private String qty;
@SerializedName("date")
@Expose
private String date;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
Now perform retrofit call like the following
private void getMonthData() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("add_base_url")
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
Call<Example> call = requestInterface.getMonths();
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
Map<String, List<MonthModel>> resultMap=response.body().getForecast().getResult();
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
}
});
}
RequestInterface.java
public interface RequestInterface {
@GET("add_your_url_endpoint")
Call<Example> getMonths();
}
I would do it with a custom Json adapter using Gson lib. So you won't have to touch your data structures (if you can not anyway) neither add your infinite key 555.
https://www.tutorialspoint.com/how-to-implement-custom-jsonadapter-using-gson-in-java