Convert JSON response into List<T>
13,082
Solution 1
JSONObject data = new JSONObject(response);
JSONArray accounts = data.getJSONArray("data");
List<Account> accountList = new Gson().fromJson(accounts.toString(), new TypeToken<ArrayList<Account>>(){}.getType());
If you cannot change your JSON response to remove the inner "data" key, you can use this:
Gson gson = new Gson();
ArrayList<Account> accountList = new ArrayList<Account>();
JSONArray accounts = data.getJSONArray("data");
for (int i = 0; i < accounts.length(); i++) {
JSONObject a = accounts.getJSONObject(i).getJSONObject("data");
accountList.add(gson.fromJson(a.toString(), Account.class));
}
Solution 2
For that you can use Tokens so that gson can understand the custom type...
TypeToken<List<Account>> token = new TypeToken<List<Account>>(){};
List<Account > accountList= gson.fromJson(response, token.getType());
for(Account account : accountList) {
//some code here for looping }
Solution 3
That nested "data"
key is pointless. If you can fix your JSON you should make this instead.
{
"data": [{
"ac_id": "000",
"user_id": "000",
"title": "AAA"
}, {
"ac_id": "000",
"user_id": "000",
"title": "AAA"
}]
}
And then this will work.
JSONObject data = new JSONObject(response);
JSONArray accounts = data.getJSONArray("data");
List<Account> accountList = new Gson()
.fromJson(accounts.toString(), new TypeToken<ArrayList<Account>>(){}.getType());
Or, again, that first "data"
isn't really necessary either.
If you can get your JSON just to be the list of Accounts...
[{
"ac_id": "000",
"user_id": "000",
"title": "AAA"
}, {
"ac_id": "000",
"user_id": "000",
"title": "AAA"
}]
This will work
List<Account> accountList = new Gson()
.fromJson(response, new TypeToken<ArrayList<Account>>(){}.getType());
Author by
user3478224
Updated on June 19, 2022Comments
-
user3478224 almost 2 years
I am new to GSON. I need to convert the following JSON response into a List.
JSON response:
{ "data": [{ "data": { "ac_id": "000", "user_id": "000", "title": "AAA" } }, { "data": { "ac_id": "000", "user_id": "000", "title": "AAA" } }] }
I have a class to cast data
Account. java
public class Account { public int ac_id; public int user_id; public String title; @Override public String toString(){ return "Account{"+ "ac_id="+ac_id+ ", user_id="+user_id+ ", title="+title+'}'; } }
When I cast the response with my class I get:
[Account{ac_id="000", user_id="000", title="AAA"}, Account{ac_id="000", user_id="000", title="AAA"}]
Now I need to put these two values into a
List<Account>
.
What do you suggest? -
OneCricketeer over 7 yearsI'm not sure this will work with the nested
"data"
key -
user3478224 over 7 yearsI get and JsonSyntaxException Expected BEGIN_ARRAY but was BEGIN_OBJECT
-
user3478224 over 7 yearsJsonSyntaxException Expected BEGIN_ARRAY but was BEGIN_OBJECT
-
user3478224 over 7 yearsJsonSyntaxException Expected BEGIN_ARRAY but was BEGIN_OBJECT
-
OneCricketeer over 7 yearsLike I said - It's up to you to fix the actual JSON. Don't just copy the Java code I gave.
-
OneCricketeer over 7 yearsOr you could not use Gson and parse the data yourself.
-
Elias N over 7 years@user3478224 As other users have mentioned, it's because you have a nested data key. You should have: { "ac_id": "000", "user_id": "000", "title": "AAA" } instead of { "data": { "ac_id": "000", "user_id": "000", "title": "AAA" } }
-
Elias N over 7 years@user3478224 see edit if you cannot change your JSON response
-
user3478224 over 7 yearsI appreciate your help but I can not modify my JSON string, it is working with my all classes with no problem. When I try to convert into an object array is when crashes.
-
user3478224 over 7 yearsHow can I parse the data?
-
OneCricketeer over 7 yearsUse the built-in
JSONObject
class and start parsing it. stackoverflow.com/questions/9605913/… Of course, you can do the same in Gson but using the otherwise namedJsonObject
-
OneCricketeer over 7 yearsYou can't modify this one API response to not put
"data":
as part of the response? -
user3478224 over 7 yearsI will try using String.replace to remove the parts that avoid match the form you suggest.
-
OneCricketeer over 7 yearsThat's risky... I think 'Elias N' has the updated answer that should work if you can't edit the JSON on the server-side
-
koceeng over 7 yearsdid you change the JSON first? if not then you will get that error
-
koceeng over 7 yearsstackoverflow.com/users/6709464/elias-n answer was good, but for me, i think it take too much line of code. I prefer simple way more
-
user3478224 over 7 yearsThank you so much @EliasN your answer works perfect!