【问题标题】:Get JSONObject from retrofit2 response从 retrofit2 响应中获取 JSONObject
【发布时间】:2017-10-13 23:00:48
【问题描述】:

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

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;

   }

我想得到

JSON 对象信息

像一个普通的对象。

【问题讨论】:

    标签: android retrofit retrofit2 pojo okhttp3


    【解决方案1】:

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

    public static class Info {
    
        @SerializedName("description")
        public String mDescription;
        @SerializedName("preview_image")
        public String mPreviewImage;
    
    }
    

    在 MyResponseObject 中:

    @SerializedName("info")
    public Info info;
    

    【讨论】:

    • 但我不知道 JSONObject 长什么样子
    【解决方案2】:

    我不知道JSONObject,但你可以试试Observable&lt;Response&lt;JsonElement&gt;&gt;,它有类似的API。

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

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

    【讨论】:

    • 谢谢罗伯特! @RobertEstivill
    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多