0

Im having a little issue with parsing json date.

Here is what I would like to parse:

 {"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012     4:49:17 PM","suspended": "false","checkedin": "0"}

I am having trouble parsing "lastLatitudeUpdate" is it because there are spaces in between? Thanks in advance for the help.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
John Nguyen
  • 547
  • 1
  • 11
  • 25
  • Depends. What is the code you are using to parse it? You can always do a `String.replace("[ ]{2,}", " ");` to replace any double or more space segments. EDIT: I *think* that should work but my regex-foo may be slightly off. – Kevin Coppock May 22 '12 at 16:34

4 Answers4

1

Short answer: No, there is no way for the JSON engine to recognize a string as a Date object.

Long answer: There is no 'date' type in JSON. However, this JSON is fine, the catch is that lastLatitudeUpdate will be parsed as a string. In order to convert this to a date you should try something like

var my_object= JSON.parse({"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012     4:49:17 PM","suspended": "false","checkedin": "0"});
my_object.lastLatitudeUpdate= Date.parse(my_object.lastLatitudeUpdate)

This function will give a timestamp. However, you have to make sure the string is correctly recognized, you may have to do some extra work.

Pablo Mescher
  • 26,057
  • 6
  • 30
  • 33
  • the question is about android and therefore java. nobody ever mentioned javascript... ;) – s1lence May 22 '12 at 16:46
  • @s1lence JSON stands for Javascript Object Notation. What is true for javascript is true in any language, in this case. Have you even read my answer? – Pablo Mescher May 22 '12 at 16:53
  • yes I did. I see your argument about json not having a date type and it's totally correct. However this wasn't the question.Further on Javascript allows a slightly better version of JSON than the real json specification does. (e.g. no quotes needed around string keys) – s1lence May 23 '12 at 17:43
1

Assuming you are on Android and therefore working with java (yes you don't mention that, only the tag in your question suggests it...)

Like mentioned here (and in various other places) you can parse a date in java using the SimpleDateFormat class:

SimpleDateFormat parserSDF=new SimpleDateFormat("M/d/yyyy h:m:s a");
Date d = parserSDF.parse(dateField,0);

Of course you have to first parse you json input with some library (e.g. standard library from json.org or Google gson) and then parse the string you'll get there for the field into a date.

Community
  • 1
  • 1
s1lence
  • 2,188
  • 2
  • 16
  • 34
-2

How are you parsing the date? In Chrome this seems to work fine:

new Date("5/21/2012     4:49:17 PM");
Mon May 21 2012 16:49:17 GMT-0400 (US Eastern Daylight Time)
  • 2
    In Chrome? This question is about Android. See the tags. – mtmurdock May 22 '12 at 16:38
  • Sorry for not mentioning that I am woroking with Java. These are all quick replies! s1lence's answer worked out for me. I never knew there was such a class. Thanks again! – John Nguyen May 22 '12 at 17:00