JScript Methods (Windows Sc ...


 Collapse AllExpand All        Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: JScript 
JSON.parse Method (Windows Scripting - JScript)

Updated: August 2008

Deserializes JavaScript Object Notation (JSON) text to produce a JScript value.

JSON.parse(text [, reviver])
Arguments

text

Required. Valid JSON text.

reviver

Optional. A function that filters and transforms the results. The deserialized object is traversed recursively, and the reviver function is called for each member of the object in post-order (every object is revived after all its members have been revived). For each member, the following occurs:

  • If reviver returns a valid value, the member value is replaced with the value returned by reviver.

  • If reviver returns what it received, the structure is not modified.

  • If reviver returns null or undefined, the object member is deleted.

The reviver argument is often used to transform JSON representation of International Organization for Standardization (ISO) date strings into Coordinated Universal Time (UTC) format Date objects.

Return Value

A JScript value—an object or array.

Exceptions

Exception

Condition

JScript parser errors

The input text does not comply with JSON syntax. To correct the error, do one of the following:

  • Modify the text argument to comply with JSON syntax. For more information, see the BNF syntax notation of JSON objects.

  • Make sure that the text argument was serialized by a JSON-compliant implementation, such as, JSON.stringify.

Example

This example uses JSON.parse to deserialize JSON text into the contact object.

JScript
var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = JSON.parse(jsontext);
var fullname = contact.surname + ", " + contact.firstname;
// The value of fullname is "Aaberg, Jesper"

This example uses JSON.parse to deserialize an ISO-formatted date string. The dateReviver function returns Date objects for members that are formatted like ISO date strings.

JScript
var jsontext = '{ "hiredate": "2008-01-01T12:00:00Z", "birthdate": "2008-12-25T12:00:00Z" }';
var dates = JSON.parse(jsontext, dateReviver);
var string = dates.birthdate.toUTCString();
// The value of string is "Thu, 25 Dec 2008 12:00:00 UTC"

function dateReviver(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;
};
Requirements

Version 5.8

NoteNote:

Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.

Internet Explorer 8 opts into the JScript 5.8 language features when the document mode for Internet Explorer 8 is "Internet Explorer 8 Standards" mode. For other document modes, Internet Explorer uses the version 5.7 feature set.

JScript 5.8 includes native JavaScript Object Notation (JSON) support and the accessor methods for Document Object Model (DOM) prototypes.

See Also

Reference

Change History

Date

History

Reason

August 2008

Added topic.

Information enhancement.

Tags :


Community Content

mpcref
JSON in ASP?
How can I invoke a call to this SetProperty method in order to activate the JScript 5.8 feature set from ASP?
I wish to use the JSON object but it is not available by default.


Page view tracker