3

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?

LOZ
  • 1,169
  • 2
  • 16
  • 43
vikke
  • 127
  • 2
  • 14

4 Answers4

3

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]");
1

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.

vikke
  • 127
  • 2
  • 14
1

under flexjson 1.9.2 below

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

ALQH
  • 151
  • 2
  • 10
-3

try this. Its working with me.

JavaScriptSerializer jss = new JavaScriptSerializer();

List<Book> lstBook = jss.Deserialize<List<Book>>("your string");
Dips
  • 228
  • 2
  • 4
  • 22
  • 2
    Isnt JavaScriptSerializer a C# class? If it is available for Java, what lib do I need to import? – vikke Aug 14 '12 at 05:54
  • Yup, Its a C# class, for java try this link http://code.google.com/p/json-io/. I have not tested it but seems it will work. – Dips Aug 14 '12 at 15:13