开发者

How to get List<Long> from json string using FlexJson Json serializer in Java?

String episod开发者_运维技巧eIds = "['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)


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,


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).


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 ]" ??
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜