JavaScript JSON.stringify ()
JSON.stringify () method is used to convert JavaScript JSON string value.
grammar
JSON.stringify(value[, replacer[, space]])
Parameter Description:
- value:
Necessary, a valid JSON string.
- replacer:
Optional. Results for switching functions or arrays.
If replacer is a function, then JSON.stringify will call the function, passing the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the exclusion of members. Key root object is an empty string: "."
If replacer is an array, the array having a key member of the only conversion. Conversion Order members and key in an array of the same order. When the value parameter is also an array, the array will be ignored replacer.
- space:
Optionally, add text indentation, spaces and line breaks, if space is a number, the return value of the specified number of spaces to indent text in each level, if space is greater than 10, the text is indented 10 spaces. There are non-numeric space can be used, such as: \ t.
return value:
Returns a string containing JSON text.
Examples
Examples
str_pretty1 = JSON.stringify (str)
document.write ( "only one parameter case:");
document.write ( "<br>");
document.write ( "<pre>" + str_pretty1 + "</ pre>");
document.write ( "<br>");
str_pretty2 = JSON.stringify (str, null, 4) // use four spaces to indent
document.write ( "parameter case:");
document.write ( "<br>");
document.write ( "<pre>" + str_pretty2 + "</ pre>"); // pre formatted for output
try it"