【发布时间】:2019-01-17 06:49:59
【问题描述】:
我见过很多类似的问题。我找不到任何适合我的确切问题的东西。在所有示例中,我发现 List 是在父类中定义的对象类型,而我只有一个字符串列表。我尝试过使用简单的数组 String[],并且看到了重载反序列化器并获取 TypeToken 的示例,但我无法将其绑定在一起以使其工作。我的列表始终为空(如果我在定义列表时未对其进行初始化,则为 null)。我在这里缺少什么,感觉就像我正在尝试做一些非常简单的事情,但我在上面找到的所有内容看起来都过于复杂。
这是我的课:
public class MondoConfig {
private String merchantURL;
public ArrayList<String> targets = new ArrayList<String>();
public MondoConfig () {}
public String getMerchantURL() {
return this.merchantURL;
}
public void setMerchantURL(String url) {
this.merchantURL = url;
}
public ArrayList<String> getTargets() {
return this.targets;
}
public void setTargets(ArrayList<String> t) {
this.targets = t;
}
}
这是我的 json:
{
"merchantURL":"https://example.com/collections/posters",
"targets":[
"testing",
"another",
"one more"
]
}
还有我要反序列化的代码:
BufferedReader br = new BufferedReader(new FileReader("C:\\mondo_config.json"));
MondoConfig config = gson.fromJson(br, MondoConfig.class);
【问题讨论】:
标签: java json list gson deserialization