GSON 未填充对象列表

发布于 2024-09-26 04:30:27 字数 1439 浏览 3 评论 0 原文

我尝试了几种解决方案,用GSON解析JSON的结果总是出错。

我有以下 JSON:

{
    "account_list": [
        {
            "1": {
                "id": 1,
                "name": "test1",
                "expiry_date": ""
            },
            "2": {
                "id": 2,
                "name": "test2",
                "expiry_date": ""
            }
        }
    ]
}

在我的 Java 项目中,我有以下结构:

public class Account{

    private int id;
    private String name;
    private String expiry_date;

    public Account()
    {
        // Empty constructor
    }

    public Account(int id, String name, String expiry_date)
    {    
        this.id = id;
        this.name = name;
        this.expiry_date = expiry_date;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getExpiryDate() {
        return expiry_date;
    } 
}

并且

public class AccountList{
    private List <Account> account_list;

    public void setAccountList(List <Account> account_list) {
        this.account_list = account_list;
    }

    public List <Account> getAccountList() {
        return account_list;
    }
}

我所做的反序列化是:

Data.account_list = new Gson().fromJson(content, AccountList.class);

最后我得到的 List 仅包含一个元素且值错误。你能告诉我我做错了什么吗?

谢谢。

I have tried several solutions and the result of parsing JSON with GSON always gets wrong.

I have the following JSON:

{
    "account_list": [
        {
            "1": {
                "id": 1,
                "name": "test1",
                "expiry_date": ""
            },
            "2": {
                "id": 2,
                "name": "test2",
                "expiry_date": ""
            }
        }
    ]
}

In my Java project I have the following structures:

public class Account{

    private int id;
    private String name;
    private String expiry_date;

    public Account()
    {
        // Empty constructor
    }

    public Account(int id, String name, String expiry_date)
    {    
        this.id = id;
        this.name = name;
        this.expiry_date = expiry_date;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getExpiryDate() {
        return expiry_date;
    } 
}

and

public class AccountList{
    private List <Account> account_list;

    public void setAccountList(List <Account> account_list) {
        this.account_list = account_list;
    }

    public List <Account> getAccountList() {
        return account_list;
    }
}

And what I do to deserialize is:

Data.account_list = new Gson().fromJson(content, AccountList.class);

At the end I'm getting List with only one element and with wrong values. Can you indicate me plase what I'm doing wrong?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2024-10-03 04:30:27

您的 javabean 结构与 JSON 结构不匹配(或反之亦然)。 JSON 中的 account_list 属性基本上包含一个带有 single 对象的数组,该对象又包含不同的 Account 属性,似乎使用索引作为属性键。但 Gson 需要一个包含多个 Account 对象的数组。

为了匹配您的 javabean 结构,JSON 应该如下所示:

{
    "account_list": [
        {"id": 1, "name": "test1", "expiry_date": ""},
        {"id": 2, "name": "test2", "expiry_date": ""}
    ]
}

如果您无法更改 JSON 结构,那么您必须更改 Javabean 结构。但由于 JSON 结构本身没有多大意义,因此很难给出适当的建议。使用 List> 而不是 AccountList 类中的 List 可以实现此目的。但如果您想将其保留为 List,那么您需要创建一个自定义 Gson 反序列化器。

Your javabean structure doesn't match the JSON structure (or the other way round). The account_list property in JSON basically contains an array with a single object which in turn contains different Account properties, seemingly using an index as property key. But Gson is expecting an array with multiple Account objects.

To match your javabean structure, the JSON should look like this:

{
    "account_list": [
        {"id": 1, "name": "test1", "expiry_date": ""},
        {"id": 2, "name": "test2", "expiry_date": ""}
    ]
}

If you can't change the JSON structure, then you have to change the Javabean structure. But since the JSON structure at its own makes little sense, it's hard to give an appropriate suggestion. A List<Map<Integer, Account>> instead of List<Account> in AccountList class will work for this. But if you'd like to keep it a List<Account>, then you need to create a custom Gson deserializer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文