Get JSONObject from retrofit2 response

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

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

      

MyResponseObject looks like this:

public class MyResponseObject {

    @SerializedName("title")
    public String title;

    @SerializedName("info")
    public JSONObject info;

    @SerializedName("question_id")
    public String questionId;

   }

      

I want to receive

JSONObject Information

like a normal object.

+3


source to share


2 answers


I don't know about JSONObject

, but you can try Observable<Response<JsonElement>>

which has a similar API.

I believe Json should be deserialized into an object JsonElement



You can also call Response.body()

or Response.errorBody()

if you just need a json string.

+1


source


You need to create another class (Info):

public static class Info {

    @SerializedName("description")
    public String mDescription;
    @SerializedName("preview_image")
    public String mPreviewImage;

}

      



and in MyResponseObject:

@SerializedName("info")
public Info info;

      

+2


source







All Articles