| ★ wanayoo — archive 1999 https://developer.mozilla.org/En/Using_native_JSON | Nouvelle recherche | Portail wanayoo |
This article covers the ECMAScript 5 compliant native JSON object added in Gecko 1.9.1. For basic information on using JSON in previous versions of Firefox, see JSON.
The native JSON object includes two key methods. The JSON.parse() method parses a JSON string, reconstructing the original JavaScript object, while the JSON.stringify() method accepts a JavaScript object and returns its JSON equivalent.
TypeError exception.To convert a JSON string into a JavaScript object, you simply pass the JSON into the JSON.parse() method, like this:
var jsObject = JSON.parse(jsonString);
To convert a JavaScript object into a JSON string, pass the object into the JSON.stringify() method:
var foo = {};
foo.bar = "new property";
foo.baz = 3;
var jsonString = JSON.stringify(foo);
jsonString now holds '{"bar":"new property","baz":3}'.
Starting in Firefox 3.5.4, JSON.stringify() offers additional customization capabilities through the use of optional parameters. The syntax is:
jsonString = JSON.stringify(value [, replacer [, space]])
valuereplacerString and Number objects that serve as a whitelist for selecting the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.spaceString or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 if it's larger than that. Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.The replacer parameter can be either a function or an array. As a function, it takes two parameters, the key and the value being stringified. Initially it gets called with an empty key representing the object being stringified, and it then gets called for each property on the object or array being stringified. It should return the value that should be added to the JSON string, as follows:
Number, the string corresponding to that number is used as the value for the property when added to the JSON string.String, that string is used as the property's value when adding it to the JSON string.Boolean, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string.replacer function on each property, unless the object is a function, in which case nothing is added to the JSON string.undefined, the property is not included in the output JSON string.replacer function.replacer function to remove values from an array. If you return undefined or a function then null is used instead.function censor(key, value) {
if (typeof(value) == "string") {
return undefined;
}
return value;
}
var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
var jsonString = JSON.stringify(foo, censor);
The resulting JSON string is {"week":45,"month":7}.
If replacer is an array, the array's values indicate the names of the properties in the object that should be included in the resulting JSON string.
Page last modified 23:07, 25 May 2010 by Neil