Search code examples
androidretrofitretrofit2pojookhttp

Get JSONObject from retrofit2 response


How to get in retrofit2 unknown JSON object from response object like this request (with OkHttp3):

Observable<Response<MyResponseObject>> apiCall(@Body body);

MyResponseObject look like this:

public class MyResponseObject {

    @SerializedName("title")
    public String title;

    @SerializedName("info")
    public JSONObject info;

    @SerializedName("question_id")
    public String questionId;

   }

I want to get

JSONObject info

like a regular object.


Solution

  • I don't know about JSONObject but you can try Observable<Response<JsonElement>> which has a similar API.

    I believe that should deserialize your Json into a JsonElement object

    You can also call Response.body() or Response.errorBody() if you just need the json String.