使用Flexjson将JSON列表反序列化为对象列表

使用Flexjson将JSON列表反序列化为对象列表

问题描述:

我正在尝试反序列化以下json:

I'm trying to deserialize the following json:

{ "books": [ {"id":"1","name":"book 1"}, {"id":"2","name":"book 2"} ] }

进入列表。以前用这个json工作了:

Into a List. It worked before with this json:

[ {"id":"1","name":"book 1"}, {"id":"2","name":"book 2"} ] }

使用此代码:

List<Book> items = new JSONDeserializer<ArrayList<Book>>()
.use("values", Book.class).deserialize(json, ArrayList.class);

但是现在看了多个例子我不知所措,是否有可能直接反序列化为列表?

But now after looking at multiple examples I am at a loss, is it possible to deserialize directly into a list?

好吧我觉得我找到了一个可以接受的解决方案,虽然我不知道它是多么优化

Okay I think I found an acceptable solution, although I do not know how optimal it is

List<Book> items = new JSONDeserializer<Map<String,List<Book>>>().
use("values.values", Book.class).deserialize(json, Map.class).get("books");

会产生一系列图书。如果有人可能有更正确的解决方案,请随时发表评论。

Will result in a list of books. If anyone maybe have a more "proper" solution feel free to comment.