我尝试了几种解决方案,使用 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 只有一个元素和错误的值。你能指出我做错了什么吗?
谢谢。