你为什么不直接使用一个?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