Alex Rivera | Logout

defining date in json file

Asked 2012-04-25T07:15:17.113
14

I wonder to know how to set today's date in json file like we are using in js.

Is there any option to specify Date.today() in json file?

Because, json data has date object which specifies system date whenever we read the json file.

Hope, you will understand what i am trying to say.

Thanks in advance,

-Raja.

Edit
Report

1 Answer

5

I suggest that you consider using the JSON-js (json2.js) parser, because it parses all standard JSON, but also allows you to add custom parse handling logic, called a reviver function, which fits your scenario very well. The basic syntax to invoke the JSON parser with a custom handler looks like this:

var myObject = JSON.parse(myJSONtext, reviverFunction);

Using your example input as a guide, it could be set up to work like this:

var jsonTxt = '[{' +
        '"data1": "foo",' +
        '"Date":  "",' +
        '"childs": [{' +
            '"data2": "stack",' +
            '"Date": ""' +
        '}{}{}...]}]';    //and-on-and-on as in your comment

myData = JSON.parse(jsonTxt, function ( key, value ) {
    if ( key === 'Date') { return new Date(); }
    //any additonal custom logic you may need later...
});

A general introduction to JSON-js is provided at the JSON in JavaScript page, along with some brief intro info about JSON, and the page also includes some usage scenarios.

answered 2012-05-01T05:48:57.443

Your Answer