Luca Parlapiano Luca Parlapiano - 1 year ago 88
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download