Set indentation character during JSON serialization with JSON.stringify in JavaScript
Description
The following code shows how to set indentation character during JSON serialization with JSON.stringify.
Example
<!-- ww w . j av a2 s . co m-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var book = {
"title": "JavaScript",
"authors": [ "J" ],
edition: 3,
year: 2011
};
var jsonText = JSON.stringify(book, null, " - -");
document.writeln("<pre>"+jsonText+"</pre>");
</script>
</head>
<body>
</body>
</html>
The code above generates the following result.
Javascript Tutorial JSON
Add toJSON() method to object and have it r...
Control the string indention converting JSO...
Convert JSON object to String in JavaScript
Convert selected fields from JSON to String...
Convert string to date when converting stri...
Handle field one by one during JSON to stri...
Add toJSON() method to object and have it r...
Control the string indention converting JSO...
Convert JSON object to String in JavaScript
Convert selected fields from JSON to String...
Convert string to date when converting stri...
Handle field one by one during JSON to stri...