PackageTop Level
Classpublic final class JSON
InheritanceJSON Inheritance Object

Language Version : ActionScript 3.0
Runtime Versions : Flash Player 11.0, AIR 3.0

The JSON class lets applications import and export data using JavaScript Object Notation (JSON) format. JSON is an industry-standard data interchange format that is described at http://www.json.org.

As a rule, the ActionScript JSON class adheres to the ECMA-262 specification. The following exceptions occur where ActionScript provides greater flexibility than ECMAScript:

For most ActionScript classes, the ActionScript JSON.stringify() method generates a default encoding. ActionScript classes can change this encoding by defining a toJSON() method in the class or its prototype. For a few ActionScript classes, the default JSON encoding is inappropriate. These classes provide a trivial, overridable implementation of toJSON() that returns the value described in the following table. You can override or redefine the toJSON() methods on these classes if you require a specific behavior. The following table describes these exceptions:

ClassComments
ByteArrayOverridable toJSON() method returns the string "ByteArray".
DictionaryOverridable toJSON() method returns the string "Dictionary".
DateOverridable toJSON() method returns Date.toString(). This exception guarantees that the ActionScript Date constructor can parse the JSON string.
XMLOverridable toJSON() method returns the string "XML".



Public Methods
 MethodDefined By
 Inherited
_init():*
[static]
_init
 Inherited
AS3
 Inherited
init():*
[static]
init
 Inherited
AS3
  
parse(text:String, reviver:Function = null):Object
[static] Accepts a JSON-formatted String and returns an ActionScript Object that represents that value.
JSON
 Inherited
AS3
  
stringify(value:Object, replacer:* = null, space:* = null):String
[static] Returns a String, in JSON format, that represents an ActionScript value.
JSON
Protected Methods
 MethodDefined By
 Inherited
[static]
protected
 Inherited
_setPropertyIsEnumerable(o:*, V:String, enumerable:Boolean):void
[static]
protected
Public Constants
 ConstantDefined By
 Inheritedlength : int = 1
[static]
length
Method Detail
parse()method
public static function parse(text:String, reviver:Function = null):Object

Language Version : ActionScript 3.0
Runtime Versions : Flash Player 11, AIR 3.0

Accepts a JSON-formatted String and returns an ActionScript Object that represents that value. JSON objects, arrays, strings, numbers, Booleans, and null map to corresponding ActionScript values, as shown below:

JSON typeActionScript type
arrayArray
stringString
numberNumber
booleanBoolean
nullnull

The reviver parameter is a function that takes two parameters: a key and a value. You can use this function to transform or filter each key/value pair as it is parsed. If you supply a reviver function, your transformed or filtered value for each pair, rather than the default parsing, is returned in the parse() function output. If the reviver function returns undefined for any pair, the property is deleted from the final result.

When the argument passed to the reviver parameter is not a function, ActionScript throws a TypeError with error ID kJSONInvalidReviver. It also posts a localized error message.

If the parse() function encounters duplicate keys within an object, the duplicate key encountered last provides the value for all preceding occurrences of that key.

Parameters

text:String — The JSON string to be parsed
 
reviver:Function (default = null) — (Optional) A function that transforms each key/value pair that is parsed

Returns
Object
stringify()method 
public static function stringify(value:Object, replacer:* = null, space:* = null):String

Language Version : ActionScript 3.0
Runtime Versions : Flash Player 11, AIR 3.0

Returns a String, in JSON format, that represents an ActionScript value. The stringify method can take three parameters.

The value parameter is required. This parameter is an ActionScript value. Typically, it is an Object or Array, but it can also be a String, Boolean, Number, or null.

The optional replacer parameter can be either a function or an array of strings or numbers. If it is a function, the function takes two parameters: a key and a value. You can use this function to transform or filter each key/value pair as it is parsed. If you supply a replacer function, your transformed or filtered value for each pair, rather than the default parsing, is returned in the parse() function output. If the replacer function returns undefined for any pair, the property is deleted from the final result. If replacer is an array, it is used as a filter for designating which properties are included in the output.

When the argument passed to the replacer parameter is neither an array or a function, ActionScript throws a TypeError with error ID kJSONInvalidReplacer. It also posts a localized error message.

The optional space parameter is a String or Number that allows white space to be injected into the returned string to improve human readability. Entries in generated JSON objects and JSON arrays are separated by a gap derived from the space parameter. This gap is always 0 to 10 characters wide. If space is a string, then the derived gap is the first ten characters of that string. If space is a non-negative number x, then the gap is x space characters, to a maximum of ten spaces. When the argument passed to the space parameter is not a String a Number, it is converted to a String for use as the gap in the output. Execution then proceeds.

When the stringify() method encounters a cyclic structure, it throws a TypeError with error ID kJSONCyclicStructure. It also posts a localized error message.

Parameters

value:Object — The ActionScript value to be converted into a JSON string
 
replacer:* (default = null) — (Optional) A function or an array that transforms or filters key/value pairs in the stringify output
 
space:* (default = null) — (Optional) A string or number that controls added white space in the returned String

Returns
String