Reputation: 14367
Trying out Gson for the first time instead of looping through the JSON objects for speed.
This is my input data set for parsing
{
"data": [
{
"access_token": "XXXXX",
"category": "Community",
"name": "Startup notes by Vrashabh",
"id": "XXXXX",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
{
"access_token": "XXXX",
"category": "Community",
"name": "Clean Bangalore",
"id": "XXXXX",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
{
"access_token": "XXXXX",
"category": "Internet/software",
"name": "Getmeetin",
"id": "XXXXX",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
}
],
"paging": {
"cursors": {
"before": "MTU3MzE3MTA0MjkyMjY4MQ==",
"after": "MjcyMTIwMzE2Mjk3NzI5"
}
}
}
And this is my gson mapping class
public class AccountsResponse {
ArrayList<AcResponseData> data;
ArrayList<PagingData> paging;
public class AcResponseData {
public String access_token;
public String category;
public String name;
public String id;
public String[] perms;
}
public class PagingData{
public Cursors cursors;
}
public class Cursors{
public String before;
public String after;
}
}
Code for parsing the data
AccountsResponse responseAccounts = gsonResponse.fromJson(response.getRawResponse(), AccountsResponse.class);
I Know I am supposed to not expect magic in terms of data conversion, I found the other questions on SO that ask me to implement TypeToken but I couldn't get it working for this case. How do I use TypeToken to get this data into the ORM I wouldn't mind not reading that paging data also actually, if that needs to be eliminated from the ORM
UPDATE Changed the ORM as below but now I get
java.lang.StackOverflowError
Upvotes: 1
Views: 108
Reputation: 2764
The problem is in your AccountsResponse
class. It should be an object PagingData
not an ArrayList
, because from the json response in your question, "paging" is a JSON object not a JSON array. So, you should declare paging
as a PagingData
object not as an ArrayList
of PagingData
objects. That should fix it.
public class AccountsResponse {
ArrayList<AcResponseData> data;
PagingData paging;
public class AcResponseData {
public String access_token;
public String category;
public String name;
public String id;
public String[] perms;
}
public class PagingData{
public Cursors cursors;
}
public class Cursors{
public String before;
public String after;
}
}
Let me know if this helps.
Upvotes: 2