I am attempting to parse the following JSON response with GSON:
{
"total": 15,
"page": 2,
"pagesize": 10,
"rep_changes": [
{
"user_id": 2341,
"post_id": 2952530,
"post_type": "question",
"title": "unprotected access to member in property get",
"positive_rep": 5,
"negative_rep": 0,
"on_date": 1275419872
},
{
"user_id": 2341,
"post_id": 562948,
"post_type": "question",
"title": "Do you have any recommendations on Blend/XAML books/tutorials for designers?",
"positive_rep": 20,
"negative_rep": 0,
"on_date": 1270760339
}
....
}
These are the two classes I have defined (each class contains the respective getters and setters):
public class Reputation {
private int total;
private int page;
private int pagesize;
private List<RepChanges> rep_changes;
public class RepChanges {
private int user_id;
private int post_id;
private String post_type;
private String title;
private int positive_rep;
private int negative_rep;
private long on_date;
I have been unable to parse the RepChanges class into the rep_changes attribute in the Reputation class. Any thoughts?
Note: I have managed to parse total, page, and pagesize (these are non-complex attributes). This is what I have done:
To parse the total, page, and pagesize, I used (and worked fine):
Gson gson = new Gson();
Reputation rep = gson.fromJson(jsonText, Reputation.class);
//doing this works:
int page = rep.getPage();
System.out.println(page);
However, when doing:
List<RepChanges> rep_changes = rep.getRep_changes();
for(RepChanges r : rep_changes){
System.out.println(r.toString());
}
I (technically) do not get an error, but the following (which seems like a memory location):
stackoverflow.objects.RepChanges@116bb691
In your
Repchangesclass, configure your owntoString()method similar to this: