Convert JSON response into List<T>

纵然是瞬间 提交于 2020-01-03 16:57:01

问题


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?


回答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));
}



回答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  }



回答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());



回答4:


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());


来源:https://stackoverflow.com/questions/41249123/convert-json-response-into-listt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!