Global.JSON

Global JSON object providing the parse and stringify methods.

  • 0.8
  • 0.8
  • 0.8
Defined By

Methods

Global.JSON
( text, reviver ) : Object
Parses a JSON text to produce an object or array. ...

Parses a JSON text to produce an object or array.

The parse method throws a SyntaxError exception if the text cannot be parsed.

The optional reviver parameter is a function that can filter and transform the results. It receives each of the keys and values, and its return value is used instead of the original value. If it returns what it received, then the structure is not modified. If it returns undefined then the member is deleted.

Examples

Parse with Reviver Function

The following example parses the text, converting  values that look like ISO date strings into Date objects.
myData = JSON.parse(text, function (key, value) { var a; if (typeof value === 'string') { a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); if (a) { return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])); } } return value; });

Parameters

  • text : String

    JSON text to parse.

  • reviver : Callback

    Function to filter and transform the results.

Returns

  • Object
Global.JSON
( value, [replacer], [space] ) : String
Produces a JSON text from a JavaScript value. ...

Produces a JSON text from a JavaScript value.

When an object value is found, if the object contains a toJSON method, its toJSON method is called and the result is stringified. A toJSON method does not serialize: it returns the value represented by the name/value pair that should be serialized, or undefined if nothing should be serialized. The toJSON method is passed the key associated with the value, and this is bound to the value.

For example, this would serialize Dates as ISO strings.

Date.prototype.toJSON = function (key) {
    function f(n) {
        // Format integers to have at least two digits.
        return n < 10 ? '0' + n : n;
    }

    return this.getUTCFullYear()   + '-' +
         f(this.getUTCMonth() + 1) + '-' +
         f(this.getUTCDate())      + 'T' +
         f(this.getUTCHours())     + ':' +
         f(this.getUTCMinutes())   + ':' +
         f(this.getUTCSeconds())   + 'Z';
};

You can provide an optional replacer function. It is passed the key and value of each member, with this bound to the containing object. The value that is returned from your function is serialized. If your function returns undefined, then the member will be excluded from the serialization.

If the replacer parameter is an array of strings, then it is used to select the members to be serialized. It filters the results such that only members with keys listed in the replacer array are stringified.

Values that do not have JSON representations, such as undefined or functions, will not be serialized. Such values in objects are dropped; in arrays they are replaced with null. You can use a replacer function to replace those with JSON values.

JSON.stringify(undefined) returns undefined.

The optional space parameter produces a stringification of the value that is filled with line breaks and indentation to make it easier to read.

If the space parameter is a non-empty string, then that string is used for indentation. If the space parameter is a number, then each level is indented by the specified number of spaces.

Examples

Formatting with a Replacer Function

The following example produces a readable version of a Titanium event object, omitting the source and type properties and adding whitespace for readability:

JSON.stringify(evt, function(key, value) {
    if(key === 'source' || key === 'type') {
        return undefined;
    } else {
        return value;
    }
}, 2));

Parameters

  • value : Object

    Any JavaScript value, usually an object or array.

  • replacer : Callback/Array<String> (optional)

    Determines how object values are stringified for objects. See main text for usage.

  • space : Number/String (optional)

    Specifies how nested structures are indented. If it is a number, it specifies the number of spaces to indent at each level. If it is a string (such as '\t' or '&nbsp;'), it specifies the characters used to indent at each level.

Returns

  • String