Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
SyntaxJSON.stringify( value [ , replacer] [ , space] )
A string that contains the JSON text.
ExamplesThis example uses JSON.stringify to convert the contact
object to JSON text. The memberfilter
array is defined so that only the surname
and phone
members are converted. The firstname
member is omitted.
var contact = new Object();
contact.firstname = "Jesper";
contact.surname = "Aaberg";
contact.phone = ["555-0100", "555-0120"];
var memberfilter = new Array();
memberfilter[0] = "surname";
memberfilter[1] = "phone";
var jsonText = JSON.stringify(contact, memberfilter, "\t");
document.write(jsonText);
This example uses JSON.stringify with an array. The replaceToUpper
function converts every string in the array to uppercase.
var continents = new Array();
continents[0] = "Europe";
continents[1] = "Asia";
continents[2] = "Australia";
continents[3] = "Antarctica";
continents[4] = "North America";
continents[5] = "South America";
continents[6] = "Africa";
var jsonText = JSON.stringify(continents, replaceToUpper);
function replaceToUpper(key, value) {
return value.toString().toUpperCase();
}
This example uses the toJSON method to convert string values to uppercase.
var contact = new Object();
contact.firstname = "Jesper";
contact.surname = "Aaberg";
contact.phone = ["555-0100", "555-0120"];
contact.toJSON = function(key)
{
var replacement = new Object();
for (var val in this)
{
if (typeof (this[val]) === 'string')
replacement[val] = this[val].toUpperCase();
else
replacement[val] = this[val]
}
return replacement;
};
var jsonText = JSON.stringify(contact);
document.write(jsonText);
// Output:
{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}
'{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}'
*/
Remarks
If value has a toJSON method, the JSON.stringify function uses the return value of that method. If the return value of the toJSON method is undefined , the member is not converted. This enables an object to determine its own JSON representation.
Values that do not have JSON representations, such as undefined , will not be converted. In objects, they will be dropped. In arrays, they will be replaced with null.
String values begin and end with a quotation mark. All Unicode characters may be enclosed in the quotation marks except for the characters that must be escaped by using a backslash. The following characters must be preceded by a backslash:
During the serialization process, if a toJSON method exists for the value argument, JSON.stringify first calls the toJSON method. If it does not exist, the original value is used. Next, if a replacer argument is provided, the value (original value or toJSON return-value) is replaced with the return-value of the replacer argument. Finally, white spaces are added to the value based on the optional space argument to generate the final JSON text.
Exceptions Exception Condition Invalid replacer argument The replacer argument is not a function or an array. Circular reference in value argument not supported The value argument contains a circular reference. See also Other articles AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4