Loading ...
Sorry, an error occurred while loading the content.

997json2.js

Expand Messages
  • Douglas Crockford
    Apr 27, 2008
    • 0 Attachment
      The json2 methods take optional functions that can make
      transformations on the JSON structures.

      For example, suppose you want to represent dates as strings in the form

      "Date(09/09/2001)"

      Pass a replacer function to the stringify method:

      text = JSON.stringify([new Date()], function (key, value) {
      return this[key] instanceof Date ?
      'Date(' + this[key] + ')' : value;
      });

      Pass a reviver function to the parse method:

      data = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
      var d;
      if (typeof value === 'string' &&
      value.slice(0, 5) === 'Date(' &&
      value.slice(-1) === ')') {
      d = new Date(value.slice(5, -1));
      if (d) {
      return d;
      }
      }
      return value;
      });
    • Show all 15 messages in this topic