我有一个如下所示的 JSON 结构:
[
{
"id": 0,
"name": "Foo"
},
{
"id": 1,
"name": "Bar"
}
]
以及用于数据绑定的相应 Java 对象:
public class Thing {
public int id;
public String name;
}
我知道如何将 JSON 列表反序列化为Thing
.
现在到了棘手的部分:我想要做的是将 JSON 反序列化为一个类似于以下片段的类,只需对此类进行更改:
public class Things {
private List<Thing> things;
public void setThings(List<Thing> things) {
this.things = things;
}
public List<Thing> getThings() {
return this.things;
}
}
这是因为 JSON 反序列化是通过使用像这样的 ObjectMapper 在我们的应用程序中构建的:
private static <T> T parseJson(Object source, Class<T> t) {
TypeReference<T> ref = new TypeReference<T>() {
};
TypeFactory tf = TypeFactory.defaultInstance();
//[...]
obj = mapper.readValue((String) source, tf.constructType(ref));
//[...]
return obj;
}
是否有任何注释可以实现我想要的,或者我必须对映射器代码进行更改?
非常感谢,麦克法兰