5

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user3478224
  • 73
  • 1
  • 2
  • 11

4 Answers4

5
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));
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Elias N
  • 1,430
  • 11
  • 19
4

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  }
MezzDroid
  • 560
  • 4
  • 11
1

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());
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

if you have access to where the JSON was created, i think you should make it like this:

{"data":[{"ac_id":"000","user_id":"000","title":"AAA"},{"ac_id":"000","user_id":"000","title":"AAA"}]}

then to convert it, just use this code: (where jsonString is the string above)

List<Account> accountList = new Gson().fromJson(jsonString, new TypeToken<ArrayList<Account>>(){}.getType());
koceeng
  • 2,169
  • 3
  • 16
  • 37