Luca Parlapiano Luca Parlapiano - 4 months ago 17x
JSON Question

How to parse JavaScrips DataObject from JSON with Android?

I have this field in the Json that i receive from the server


The problem is, how i cast in the string? This is the first time that i see this field.

Thanks for any help!
I'm a newbie from Android platform.


The problem is, how i cast in the string?

You don't, you convert it to a Date (or whatever form of date object you use in your app; a lot of people don't like Date) by extracting the number from that string and using it as milliseconds-since-the-Epoch (Jan 1st 1970 at midnight GMT).

Date has a constructor for that:

String str = "/Date(1459461600000)/";
long theNumber = Long.parseLong(str.substring(6, str.length() - 2));
Date dt = new Date(theNumber);

Live example on ideone

Whatever JSON library you're using may support using a replacer of some kind so you can do that during the parsing process rather than after-the-fact.