mantc_sdr
mantc_sdr

Reputation: 491

Retrofit request response to model object parse

I am consuming an API through Retrofit2, and I want to parse/transform the response to my model type object. The thing is that the recieved answer in Json is like this:

enter image description here

...

Then, my model object has this structure:

public class ApiTokenResponse {

@SerializedName("data")
@Expose
private JsonArray data;

@SerializedName("id")
@Expose
private int id;

The problem is that I am just focused on receiving the id, not the jsonarray called data, but the only value I receive is the data array, the id is null. Do I have to later iterate the data array and get the id value or is there any way to get it directly?

Thanks

Upvotes: 0

Views: 593

Answers (1)

Amin Qayyum98
Amin Qayyum98

Reputation: 48

Your model structure has a problem. You most make your model like that.

public class Response {
    private List<Info> data;
}

And

public class Info {
    private int id;
    private String created_at;
    private String updated_at;
    private String deleted_at;
}

Note: if your JSON tag name is the same as your java variable, you can not use @SerializedName.

Note: if retrofit can't find or pars a @SerializedName in model make it NULL.

Upvotes: 1

Related Questions