从 retrofit2 响应中获取 JSONObject

Get JSONObject from retrofit2 response

如何从像这样的请求(使用 OkHttp3)的响应对象中获取 retrofit2 未知的 JSON 对象:

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

MyResponseObject 看起来像这样:

public class MyResponseObject {

    @SerializedName("title")
    public String title;

    @SerializedName("info")
    public JSONObject info;

    @SerializedName("question_id")
    public String questionId;

   }

我想得到

JSONObject info

像一个普通的物体。

您需要创建另一个 class(信息):

public static class Info {

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

}

在 MyResponseObject 中:

@SerializedName("info")
public Info info;

我不知道 JSONObject,但你可以试试 Observable<Response<JsonElement>>,它有一个类似的 API。

我认为应该将您的 Json 反序列化为 JsonElement 对象

如果您只需要 json 字符串,您也可以调用 Response.body()Response.errorBody()