使用Flexjson将JSON列表反序列化为对象列表 [英] Deserializing JSON list into list of objects with Flexjson

查看:190
本文介绍了使用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.

这篇关于使用Flexjson将JSON列表反序列化为对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆