Lion789 Lion789 - 2 months ago 16
Java Question

How to grab JSON Array and use gson to parse each json object? (Retrofit)

I am returning an array of results with my json Objects, and I am trying to use my customObjectResponse class to pull out each of the fields within each of the objects... the problem it is expecting an object so how do I edit my class to allow it to take in an array of object to be able to then call the fields of each object... I am confused as to what needs to be added:

Here is a response example of what is being passed to be used:

[ { itemId: 'dfsdfsdf343434',
name: 'tests',
picture: '6976-7jv8h5.jpg',
description: 'testy.',
dateUpdated: 1395101819,
} ]


Here is my response Object Class:

public class ObjResponse{
private String itemId;
private String name;
private String picture;

private String description;

private String location;
private int dateUpdated;

private String msg;




//gridview constructor
public ObjResponse(String picture) {
this.picture = picture;
}

//public constructor
public ObjResponse() {

}

public String getItemId() {
return itemId;
}

public void setItemId(String itemId) {
this.itemId = itemId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPicture() {
return picture;
}

public void setPicture(String picture) {
this.picture = picture;
}


public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}


public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}



public int getDateUpdated() {
return dateUpdated;
}

public void setDateUpdated(int dateUpdated) {
this.dateUpdated = dateUpdated;
}




public String getMsg() {
return msg;
}

}


what I am trying, but is not working, even if I separate the classes into their own files:

Data passed in:
items: [{obj1: "A", obj2: ["c", "d"]}, {etc...}]


public class Response {

public class List<Custom> {
private List<Custom> items;
}

public class Custom {
private String obj1;
private List<Obj2> obj2;
}

public Class Obj2 {
private String letters;
}
}

Answer

I ended up just calling in the callback a list of the customObject and it did the job...

new Callback<List<ObjResponse>>() {
Comments