views:

79

answers:

2

I am trying to the timeline chart: http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html#Data_Format Data is coming in the form of a JSON feed.

Google wants the data as something like this:

    {
   version:'0.6',
   reqId:'0',
   status:'ok',
   sig:'4641982796834063168',
   table:{
      cols:[
         {
            id:'A',
            label:'NEW A',
            type:'string'
         },
         {
            id:'B',
            label:'B-label',
            type:'number'
         },
         {
            id:'C',
            label:'C-label',
            type:'datetime'
         }
      ],
      rows:[
         {
            c:[
               {
                  v:'c'
               },
               {
                  v:3.0,
                  f:'3'
               },
               {
                  v:new Date(2008,
                  3,
                  30,
                  0,
                  31,
                  26                  ),
                  f:'4/30/08 12:31 AM'
               }
            ]
         }
      ]
   }
}

How can I output the Date function without it being wrapped in string delimiters like 'Date()'.

A: 

I simply wrapped the functions in %% characters like so:

$something = array('%%new Date(...) %%','somevalue');
$json = json_encode($something);

And removed these %% characters and the string delimiters next to them.

$json = preg_replace("/(('|\")%%|%%(\"|'))/",'', $json);
Keyo
A: 

Unfortunately, there is no "date literal" in JavaScript (arrays can be expressed with [] and objects with {}, but no such love for Date objects). As well, actual, valid JSON only accepts primitive values (like strings, numbers, arrays, booleans, objects and not much else). You may also be surprised to learn that the Date() in JSON is not valid (though that's not a problem if you don't care about portability).

If you have control over the code that produces the feed and consumes it, though, you may want to do one of a couple things. First, pass the date as a timestamp. This is easy enough:

var dtDateTime = new Date('Jan 27 2011 00:00:00 GMT+0000');
var intDateTime = dtDateTime.getTime();
var objJSON = {
    "datetime":intDateTime
};

After loading the JSON, your code would then parse the datetime with:

var dtDateTime = new Date();
dtDateTime.setTime(objJson.datetime);

Here, your code would have to expect the datetime property and know to decode it. So it's not a great generalized solution.

Another way I've seen this nut cracked is with a special string literal, that signifies to your script that it is a datetime. It could be in the value:

var objJSON = {
    "datetime":"@Jan 27 2011 00:00:00GMT+0000@"
};

Or it could be name:

var objJSON = {
    "@datetime":1296086400000
};

The @ simply acts as a flag to your code that the value needs some sort of post-processing. Both will pass validation.

JSON is meant to be simple and cross-platform, so anything that is specific to JS is inherently bad. If you tried to load JSON into, say Java or C#, you'd have a problem.

Andrew