有以下 json 字符串:
[{"Custom":{"id":4,"name":"Lebensmittel","currency":"EUR","count":2}}]
这是一个缩短的版本,通常我有一个很长的 json 字符串,其中包含很多“自定义”对象。 我从不受我影响的远程计算机接收 json 字符串。 我正在尝试使用以下代码来反序列化 json 字符串:
Gson gson = new Gson();
Custom[] data = gson.fromJson(json, custom[].class);
结果是Array数据的大小是正确的,但是内容为空,没有值被反序列化。
这是自定义的 POJO 类
public class Custom implements Serializable {
private static final long serialVersionUID = 1826747252056159012L;
private int id;
private String name;
private String currency;
private int count;
public Custom() {
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getCurrency() {
return currency;
}
public void setCurrency(final String currency) {
this.currency = currency;
}
public int getCount() {
return count;
}
public void setCount(final int count) {
this.count = count;
}
}
那么,谁能告诉我什么是wrog?
提前致谢
最佳答案
您的 json 存在错误:
[{"Custom":{"id":4,"name":"Lebensmittel","currency":"EUR","count":2}}]
必须是
[{"id":4,"name":"Lebensmittel","currency":"EUR","count":2}]
关于java - gson 不会反序列化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37188243/