【问题标题】:Parsing JSON array with Array of objects inside with Retrofit使用 Retrofit 解析带有对象数组的 JSON 数组
【发布时间】:2020-07-02 03:16:59
【问题描述】:

我有一个来自端点的 json 响应片段,我需要在其中获取一些新闻,但是返回给我的信息有点错误,或者我做错了:

{
    "data": [
        {
            "alignImages": "left",
            "data": [
                {
                    "content": "Despus de dos aos de la operacin y una intensiva terapia, jvenes que no podan mover sus miembros inferiores y superiores ahora pueden comer, cepi",
                    "id": 179,
                    "title": "Ciruga que reconecta los nervios le devolvi a 13 tetrapljicos la movilidad en",
                    "url_detail": "https://www.elespectador.com/noticias/salud/cirugia-que-reconecta-los-nervios-le-devolvio-13-tetraplejicos-la-movilidad-en-brazos-y-codos-articulo-869",
                    "url_image": "https://storage.googleapis.com/sitidoctor-161813.appspot.com/images/noticia_2.png"
                },
                {
                    ...
                }
            ],
            "imgHeader": "https://storage.googleapis.com/sitidoctor-161813.appspot.com/images/Noticias_Salud.png",
            "titleHeader": "Noticias Salud"
        }
    ],
    "message": "success!",
    "status": 200
}

这是我用来解析改造实例查询响应的模型:

public class NewsResponse {

    @SerializedName("data")
    @Expose
    private List<New> data = null;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("status")
    @Expose
    private Integer status;

    public List<New> getData() {
        return data;
    }

    public void setData(List<New> data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

}

我使用改造提出请求,如下所示:

private void handleResponse(NewsResponse newsResponse) {
        Log.e("response", newsResponse.getData().toString());
        if (newsResponse.getData().size() > 0){
            List<New> n = new ArrayList<>();
            for (int i = 0; i < newsResponse.getData().size(); i++){
                New nn = newsResponse.getData().get(i);
                n.add(nn);
            }
            callback.onSuccess(n);
        }
}

但我总是得到一个单一的空值,就像将内部数据数组作为对象返回但无法获取数据,我尝试了一切但一无所获,有什么想法吗?

Reference imagen in debugger

【问题讨论】:

  • 查看您的调试器图像,您的 newsResponse.getData().size() 为 1。列表中唯一的元素 id、title、content、uridetail 为 null。您正在正确处理响应。你的回复有问题。请检查您是否正确拨打网络电话
  • 你是对的,它是在 api 响应中的问题,因为它给了我一个 data[] 数组而不是顶层的 data{} 对象,谢谢
  • 我会把它放在答案中,请采纳。
  • 现在你也可以投票了 :)

标签: java android arrays json retrofit


【解决方案1】:

查看您的调试器图像,您的 newsResponse.getData().size() 为 1。列表中唯一的元素 id、title、content、uridetail 为 null。您正在正确处理响应。你的回复有问题。请检查您是否正确拨打网络电话

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 2018-07-15
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2016-01-21
    • 2018-04-15
    • 2015-04-22
    相关资源
    最近更新 更多