Luca Parlapiano Luca Parlapiano - 1 year ago 68
Android Question

How to parse JavaScript's DataObject from JSON with Android?

I have this field in the JSON that I receive from the server:


How can I cast the string?

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