你为什么不只使用一个TypeReference
?
例如...
json文件test.json
在/your/path/
:
[{"s":"blah"},{"s":"baz"}]
包中的主类test
:
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
List<IBar> actuallyFoos = mapper.readValue(
new File("/your/path/test.json"), new TypeReference<List<Foo>>() {
});
for (IBar ibar : actuallyFoos) {
System.out.println(ibar.getClass());
}
}
catch (Throwable t) {
t.printStackTrace();
}
}
static interface IBar {
public String getS();
public void setS(String s);
}
static class Foo implements IBar {
protected String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
}
static class Bar implements IBar {
protected String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
}
}
方法的输出main
:
class test.Main$Foo
class test.Main$Foo