简体   繁体   中英

Parsing JSON array with Array of objects inside with Retrofit

i have this fragment of a json response from an endpoint in where i need to get some news, but is kinda returning me wrong info or maybe i'm doing this wrong:

{
    "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
}

This is the model i use to parse the response from the retrofit instance query:

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;
    }

}

and i make the request using retrofit like this:

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);
        }
}

but i'm always getting the a single and empty value, is like is returning the inner data array as object but cant get data, i tried everything and got nothing, any ideas?

Reference imagen in debugger

Looking into your debugger image, Your newsResponse.getData().size() is 1. The only element in the list has id,title,content, uridetail as null. You are handling the response correctly. There is something wrong with your response. Please check if you are making the network call correctly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM