Moti
Moti

Reputation: 482

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.

Upvotes: 3

Views: 747

Answers (2)

Robert Estivill
Robert Estivill

Reputation: 12477

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.

Upvotes: 1

user3686440
user3686440

Reputation: 46

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;

Upvotes: 2

Related Questions