0
String episodeIds = "['abc', '123', '456']";
     List<Long> list = new JSONDeserializer<ArrayList<Long>>().use(null, ArrayList.class).deserialize(episodeIds);
     System.out.println(list);

This code returns string but must return LONG)

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
alexn
  • 1
  • 1
  • 2
  • A code sample with no explanation is a bit hard to address. Is there a bug? Are you just asking for validation of your code? ...? – Marcelo Cantos Apr 02 '10 at 11:41
  • One suggestion: fix the String; JSON does not allow single quotes (unlike XML). It may be that FlexJson accepts it, but many other parser do not (rightly so). – StaxMan Apr 09 '10 at 18:08

3 Answers3

1

i don't think that you need the .use(null, ArrayList.class) part of the second line:

String episodeIds = "['abc', '123', '456']";
 List<Long> list = new JSONDeserializer<ArrayList<Long>>().deserialize(episodeIds);
 System.out.println(list);

Regards,

scriptmonster
  • 2,741
  • 21
  • 29
0

Without knowing FlexJSON better I can't be sure, but problem I would suspect is due to Java Type Erasure (google more for that if you are not familiar with the concept -- it just means that Java byte code has very little information on declared generic types, like what you do here). Because of this, library CAN NOT KNOW expected type you are giving (>, or implied return type); all it sees is ArrayList (since ArrayList.class has no generics type info); and since you are giving it JSON with Strings, it has to assume you want List of Strings.

So how to work around this? Maybe FlexJson has a way to give generics-enabled info to work around this. If not, you can sub-class ArrayList, something like 'public class MyList extends ArrayList', pass MyList.class to method. Now it SHOULD be able to determine actually expected type.

Or if that does not work, try another Java JSON library (Jackson or GSON). They can do this conversion (I know Jackson can, I would expect GSON to be able to as well).

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • One more thing: content you give is NOT JSON. JSON uses double-quotes, not single quotes. Also, 'abc' is not a number, so that'll fail. Aside from that, if you gave proper JSON content, Jackson and GSON could handle it as expected. – StaxMan Apr 09 '10 at 18:07
0

String episodeIds = "['abc', '123', '456']";

  1. 'abc' can't be converted to a number resp. long
  2. shouldn't the episode ID's in the JSON String be more like: "[ 777, 123, 456 ]" ??
t0r0X
  • 4,212
  • 1
  • 38
  • 34