我有以下的JSON实现集合类:反序列化失败与杰克逊
{
"item": [
{ "foo": 1 },
{ "foo": 2 }
]
}
这基本上是一个包含项目集合的对象。
所以我做了一个类来反序列化:
public class ItemList {
@JsonProperty("item")
List<Item> items;
// Getters, setters & co.
// ...
}
一切工作很好了这一点。
现在,为了让我的生活更容易在其他地方,我决定能够迭代ItemList对象并让它实现Collection接口会很好。
所以基本上我的课变成了:
public class ItemList implements Collection<Item>, Iterable<Item> {
@JsonProperty("item")
List<Item> items;
// Getters, setters & co.
// Generated all method delegates to items. For instance:
public Item get(int position) {
return items.get(position);
}
}
的实施工作正常,很好。然而,反序列化现在失败了。
貌似杰克逊感到困惑:
com.fasterxml.jackson.databind.JsonMappingException:不能 反序列化com.example.ItemList的情况下进行START_OBJECT令牌
我有试图添加@JsonDeserialize(as=ItemList.class)
但它并没有伎俩。
什么路要走?
我敢肯定,http://stackoverflow.com/users/22656/jon-skeet将能够回答这个问题。另外,您可以在http://stackoverflow.com/a/21279016/1382251中找到一些有用的信息。 –
虽然这是一个很好的问题,但如果你只想迭代,为什么要让你的类成为一个集合,而不是只能迭代? – chrylis
@chrylis:这样我就可以为(Item t:itemList)' –