The following response comes to the request

{ "status": "success", "message": "Успешный запрос", "code": 200, "data": { "2018-02-12": [ 36, 37, 38, 39, 40 ], "2018-02-13": [ 41, 42, 43, 44, 45 ] } } 

That is, the response data comes with variable keys: today, tomorrow will be "2018-02-13" and "2018-02-14", and so every day.

Processing jsonschema2pojo data under the key "data" yields the following model:

 package NNN.NNNN.NNNNN.model; import java.util.List; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class Data { @SerializedName("2018-02-12") @Expose private List<Integer> _20180212 = null; @SerializedName("2018-02-13") @Expose private List<Integer> _20180213 = null; public List<Integer> get20180212() { return _20180212; } public void set20180212(List<Integer> _20180212) { this._20180212 = _20180212; } public List<Integer> get20180213() { return _20180213; } public void set20180213(List<Integer> _20180213) { this._20180213 = _20180213; } } 

It is clear that such a model will become obsolete in a day and will produce empty values. Tell me, can such an answer be automatically parsed using the Retrofit library or only manually?

    2 answers 2

    Such data should be read in the Map. In this case, you need Map <String, ArrayList <Integer >>. Other data read in the fields of appropriate types. In general, a class should look like this:

     public class Response { private String status, message; private int code; private Map<String, ArrayList<Integer>> data; public Response(String status, String message, int code, Map<String, ArrayList<Integer>> data) { this.status=status; this.message=message; this.code=code; this.data=data; } public void setStatus(String status) { this.status = status; } public String getStatus() { return status; } public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } public void setCode(int code) { this.code = code; } public int getCode() { return code; } public void setData(Map<String, ArrayList<Integer>> data) { this.data = data; } public Map<String, ArrayList<Integer>> getData() { return data; } } 

    Typically, information on this topic can be found in the documentation, read carefully.

    • That is, you need to write: @SerializedName ("data") @Expose private Map <String, ArrayList <Integer >> data; and information will be automatically placed in the data field? I hope no need to manually poke around with JSONObject? And did you notice that data is not just a Map, but an ArrayList consisting of two elements of such a Map? - Pavel Sumarokov
    • Do not confuse Map and Pair, Map can contain any number of such pairs. - Andrei Chistobaev
    • Thank you. I did it all) - Pavel Sumarokov
    • Just met with the exact same problem, even JSON is similar. Is it by chance not wargaming? - Andrey Chistobaev
    • not. this project is called Cintru - Pavel Sumarokov

    enumerate object keys, write them into an array

     keys = Object.keys(obj)