Deserializing JSON list into list of objects with

2019-05-23 02:33发布

I'm trying to deserialize the following json:

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

Into a List. It worked before with this json:

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

Using this code:

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?

4条回答
小情绪 Triste *
2楼-- · 2019-05-23 02:48

Taking the sample from @vikke I successfully applied the deserialization of a list of longs.

List<Long> idsLong = new JSONDeserializer<List<Long>>().use("values", Long.class).deserialize("[123,456,789]");
查看更多
forever°为你锁心
3楼-- · 2019-05-23 02:55

under flexjson 1.9.2 below

List list = new JSONDeserializer>().use(null, ArrayList.class).use("values", Long.class).deserialize( episodeIds ); System.out.println( list );

查看更多
Summer. ? 凉城
4楼-- · 2019-05-23 03:00

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.

查看更多
Bombasti
5楼-- · 2019-05-23 03:00

try this. Its working with me.

JavaScriptSerializer jss = new JavaScriptSerializer();

List<Book> lstBook = jss.Deserialize<List<Book>>("your string");
查看更多
登录 后发表回答