1

i'm new to retrofit,below is the json

 {
  "response": "success",
  "servicecode": "134",
  "forecast": {
    "month": {
      "jan": [
        {
          "id": "1",
          "price": "12",
          "Product": "1086",
          "Qty": "14",
          "date": "2018-10-27 16:08:57"
        },
        {
          "id": "2",
          "price": "19",
          "Product": "1746",
          "Qty": "45",
          "date": "2018-10-27 16:08:57"
        }
      ],
      "april": [
        {
          "id": "3",
          "price": "89",
          "Product": "1986",
          "Qty": "15",
          "date": "2018-10-27 16:08:57"
        },
        {
          "id": "1",
          "price": "12",
          "Product": "1086",
          "Qty": "145",
          "date": "2018-10-27 16:08:57"
        }
      ],
      "jun": [
        {
          "id": "81",
          "price": "132",
          "Product": "17086",
          "Qty": "1445",
          "date": "2018-10-27 16:08:57"
        },
        {
          "id": "11",
          "price": "132",
          "Product": "10786",
          "Qty": "1445",
          "date": "2018-10-27 16:08:57"
        }
      ]
    }
  },
  "message": "Competitor Sales."
}

here, those months(jan, april, jun) are dynamic(they may come or they won't) so how to parse this using retrofit. i have no idea how to create my model class. my issue some what looks like this,but in that there's no more details. tell me a better tutorial for this.

Model class below

class ForecastViewModel {
    @SerializedName("forecast")
    Forecast[] data;

    public Forecast[] getData() { return data; }

    private class Forecast {
        @SerializedName("month")
        MonthData[] month;

        public MonthData[] getMonth() { return month; }

        private class MonthData {
            private Map<String, Pojo> forecastdata = new HashMap<>();

            private class Pojo {

                @SerializedName("id")
                @Expose
                private String pID;
                @SerializedName("price")
                @Expose
                private String pPrice;
                @SerializedName("Product")
                @Expose
                private String pProduct;
                @SerializedName("Qty")
                @Expose
                private String pQty;
                @SerializedName("date")
                @Expose
                private String pDate;

                public String getpID() {
                    return pID;
                }
                public void setpID(String pID) {
                    this.pID = pID;
                }
                public String getpPrice() {
                    return pPrice;
                }
                public void setpPrice(String pPrice) {
                    this.pPrice = pPrice;
                }
                public String getpProduct() {
                    return pProduct;
                }
                public void setpProduct(String pProduct) {
                    this.pProduct = pProduct;
                }
                public String getpQty() {
                    return pQty;
                }
                public void setpQty(String pQty) {
                    this.pQty = pQty;
                }
                public String getpDate() {
                    return pDate;
                }
                public void setpDate(String pDate) {
                    this.pDate = pDate;
                }
            }
            }
    }
}

...!!

2 Answers 2

2

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@kriss see edited answer if you want to parse using model class(method 2), first method can be used if you have access to string response
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.