java 将 JSON 响应转换为 List<T>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41249123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:46:30  来源:igfitidea点击:

Convert JSON response into List<T>

javaandroidjsonlistgson

提问by user3478224

I am new to GSON. I need to convert the following JSON response into a List.

我是 GSON 的新手。我需要将以下 JSON 响应转换为列表。

JSON response:

JSON 响应:

{
    "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?

现在我需要将这两个值放入一个List<Account>.
你有什么建议?

回答by Elias N

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:

如果您无法更改 JSON 响应以删除内部“数据”键,则可以使用:

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

回答by MezzDroid

For that you can use Tokens so that gson can understand the custom type...

为此,您可以使用令牌,以便 gson 可以理解自定义类型...

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  }

回答by OneCricketeer

That nested "data"key is pointless. If you can fix your JSON you should make this instead.

那个嵌套的"data"键是没有意义的。如果您可以修复您的 JSON,您应该改为使用它。

{
    "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...

或者,同样,这"data"也不是真正必要的。
如果你可以让你的 JSON 只是作为帐户列表......

[{
    "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());

回答by koceeng

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

如果您可以访问创建 JSON 的位置,我认为您应该这样做:

{"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)

然后转换它,只需使用此代码:(其中 jsonString 是上面的字符串)

List<Account> accountList = new Gson().fromJson(jsonString, new TypeToken<ArrayList<Account>>(){}.getType());