Client Class JSON

The JSON class provides the ability to parse JSON on the client side. This is a static object that is always available.

This class is based on Douglas Crockford's JSON JavaScript code with a number of modifications to handle date formatting in ISO and Microsoft formats. The parse() method checks for invalid characters in the JSON string before parsing and disallows executable code.

var person = { Name: "Rick", Company: "West Wind", Entered: new Date() }; var json = JSON.serialize(person); //{"Name":"Rick","Company":"West Wind","Entered":"\/Date(1199700279867)\/"} alert(json); var copied = JSON.parse(json); alert( copied.Entered + " - " + copied.Name);

Dates are formatted in Microsoft AJAX/WCF format using a string literal which looks like this: "\/Date(1199700279867)\/". The parse() method can also parse this format as well as toISOString() format proposed in JavaScript 2.0.

Class Members

MemberDescription
parse Parses a JSON string into a JavaScript value or object. This version throws an exception if parsing fails.
o.JSON.parse(text)
parseSafe Parses a JSON string into a JavaScript value or object. This version returns null on failure rather than throwing an exception.
o.JSON.parseSafe(text)
serialize Serializes a JavaScript object into a JSON string.
o.JSON.serialize(object)

Requirements

See also:

Class JSON

  Last Updated: 1/8/2008 | © West Wind Technologies, 2008