Lucas Daddiego
Lucas Daddiego

Reputation: 95

Gson: parse objects in array inside an object

i'm developing an Android app, using Retrofit, and trying to resolve this.

The backend guys until yesterday sended to me the following response:

[
    {"id":1,"key1":"value1", "key2":"value2"},
    {"id":2, "key1":"value1", "key2":"value2"}
]

Now, this wanna change that to this:

{"response":
    [
        {"id":1,"key1":"value1","key2":"value2"},
        {"id":2,"key1":"value1","key2":"value2"}
    ],
"page":1,
"count":2
}

So, basically, they send me the array with the objects inside "response", and then some meta data about paging.

Now, my question is: how I can parse that with Gson?

Currently, my code is:

The POJO:

public class MyObject{

    private int id;
    private String key1;
    private String key2;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getKey1() {
        return key1;
    }

    public void setKey1(String key1) {
        this.key1 = key1;
    }

    public String getKey2() {
        return key2;
    }

    public void setKey2(String key2) {
        this.key2 = key2;
    }
}

The Retrofit interface:

public interface Api {

    @GET("/route")
    void listObjects(Callback<List<Object>> callBack);

}

That is how I initialize Retrofit:

RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint("http://example.com")
        .setLogLevel(RestAdapter.LogLevel.FULL)
        .build();

api = restAdapter.create(Api.class);

And how I consume it:

public void getObjects(final Handler handler) {
    api.listObjects(new retrofit.Callback<List<Object>>() {
        @Override
        public void success(List<Object> objects, retrofit.client.Response response) {
            handler.onSuccess(objects);
        }

        @Override
        public void failure(RetrofitError error) {
            handler.onError();
        }
    });
}

So I wanna parse that, but I can't figure out how to do it with Retrofit and Gson.

Thanks in advance!

Upvotes: 1

Views: 1066

Answers (1)

doubleA
doubleA

Reputation: 2466

Using This site I generated these pojos.

The outer class

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;


public class Response {

@Expose
private List<Response_> response = new ArrayList<Response_>();
@Expose
private Integer page;
@Expose
private Integer count;

/**
* 
* @return
* The response
*/
public List<Response_> getResponse() {
return response;
}

/**
* 
* @param response
* The response
*/
public void setResponse(List<Response_> response) {
this.response = response;
}

/**
* 
* @return
* The page
*/
public Integer getPage() {
return page;
}

/**
* 
* @param page
* The page
*/
public void setPage(Integer page) {
this.page = page;
}

/**
* 
* @return
* The count
*/
public Integer getCount() {
return count;
}

/**
* 
* @param count
* The count
*/
public void setCount(Integer count) {
this.count = count;
}

}

The inner class

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

public class Response_ {

@Expose
private Integer id;
@Expose
private String key1;
@Expose
private String key2;

/**
* 
* @return
* The id
*/
public Integer getId() {
return id;
}

/**
* 
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}

/**
* 
* @return
* The key1
*/
public String getKey1() {
return key1;
}

/**
* 
* @param key1
* The key1
*/
public void setKey1(String key1) {
this.key1 = key1;
}

/**
* 
* @return
* The key2
*/
public String getKey2() {
return key2;
}

/**
* 
* @param key2
* The key2
*/
public void setKey2(String key2) {
this.key2 = key2;
}

}

Then the Api call looks like this.

public interface Api {

@GET("/route")
void listObjects(Callback<Response> callBack);

}

Upvotes: 2

Related Questions