Select to view content in your preferred language

JSON.encode of an date object

3963
8
04-16-2010 10:56 AM
TomSchuller
Occasional Contributor III
Hy,
running a JSON.encode on a Date object, it's generating me a non-compatible JSON-format to communicate with the AGS-Rest-api.
Here my JSON string:
{"secondsUTC":14,"date":16,"dayUTC":5,"minutes":33,"fullYear":2010,"millisecondsUTC":493,"month":3,"monthUTC":3,"seconds":14,"minutesUTC":33,"hours":20,"timezoneOffset":-120,"milliseconds":493,"hoursUTC":18,"dateUTC":16,"fullYearUTC":2010,"day":5,"time":1271442794493}


The format used in the AGS-Rest api is the date expressed in ms started from 1 Jan 1970, like this:
http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer/0/query?...

Is this a bug or what is wrong in my code below?
I'm using the com.esri.ags.utils.JSON, so it should generate the correct format.

The problem is still present in the latest beta library: agslib-2.0BetaUpdate2-2010-04-07

Thanks,
Tom


import com.esri.ags.utils.JSON;
import mx.controls.Alert;
function testJSON():void {
 var myDate:Date= new Date();
 var jsonDateString:String = JSON.encode(myDate);
 Alert.show(jsonDateString);
}
Tags (2)
0 Kudos
8 Replies
DasaPaddock
Esri Regular Contributor
You'll need to set the Date's time property in the object you're encoding.

var now:Date = new Date();
var result:String = JSON.encode({ "date": now.time });
0 Kudos
TomSchuller
Occasional Contributor III
Hy,
thanks for your answer.

Taking the case that I have a complex object which contains some date fields.
I will pass my complex object to the JSON.encode method.

How can I do this (see example below)?

Thanks,
Tom

import com.esri.ags.utils.JSON; 
import mx.controls.Alert;
function testJSON():void {
 var myObject:Object = new Object();
 myObject["label"]="test";
 myObject["myDate"]= new Date();
 var jsonDateString:String = JSON.encode(myObject);
 Alert.show(jsonDateString);
}
0 Kudos
DasaPaddock
Esri Regular Contributor
Try this:

import com.esri.ags.utils.JSON; 
import mx.controls.Alert;
function testJSON():void {
 var myObject:Object = new Object();
 myObject["label"]="test";
 myObject["myDate"]= new Date().time;
 var jsonDateString:String = JSON.encode(myObject);
 Alert.show(jsonDateString);
}
0 Kudos
TomSchuller
Occasional Contributor III
Hy,
thanks for the solution.
I'm seeing how simple it is to implements.

We are having already a lot of complex objects using date fields which where transferred with BlazeDS. We are now switching to a customSOE-Rest enabled communication.

Wouldn't it be possible to add this json-date-conversion directly to your JSON.encode method?
So we don't need to edit all our business classes.

Thanks,
Tom
0 Kudos
DasaPaddock
Esri Regular Contributor
Yes, we'll update our encoder to look for Dates and encode them using their time value.
0 Kudos
TomSchuller
Occasional Contributor III
Great, thanks.

Tom
0 Kudos
MarkDeaton
Esri Contributor
I'm also running into this issue in a .NET/winforms app that updates a feature service using REST. Since there's no JSON encoding built-in to .NET, creating a JSON-compatible date is cumbersome. Have you given any thought to allowing standardized date strings for these kinds of parameters?
0 Kudos
JeffPerkins
New Contributor III
Yes, we'll update our encoder to look for Dates and encode them using their time value.



How do I implement the JSON.encode in my flex project?   The data field that the REST returns is Thu Jun 03 18:16:36 CDT 2010 and I'd like it to be Jun 03, 2010 18:13.  

Thanks
0 Kudos