Tools, FAQ, Tutorials:
JSON-stringify-Object.html - JSON.stringify() on Objects
How JSON.stringify() behaves on JavaScript objects?
✍: FYIcenter.com
When processing an object, JSON.stringify() behave depending on the type of objects
based on the following rules:
Here is a JavaScript example showing how different types of objects get stringified:
<!-- JSON-stringify-Object.html
Copyright (c) FYIcenter.com
-->
<html>
<body>
<script type="text/javascript">
function myFunc() {
return "Hello!";
}
var myObj = new Object();
myObj.name = "Joe";
Object.defineProperty(myObj, 'age', {value: 1, enumerable: false});
Object.defineProperty(myObj, 'type', {value: 'User', enumerable: true});
var str = null;
document.write("<p>String from JSON.stringify():</p>");
document.write("<pre>");
str = JSON.stringify(new String("false"));
document.write(" String object = "+str+"\n");
str = JSON.stringify(new Boolean(false));
document.write(" Boolean object = "+str+"\n");
str = JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
document.write(" Date object = "+str+"\n");
str = JSON.stringify(myObj);
document.write(" Object object = "+str+"\n");
str = JSON.stringify(JSON);
document.write(" JSON object = "+str+"\n");
str = JSON.stringify(myFunc);
document.write(" Function object = "+str+"\n");
document.write("</pre>");
</script>
</body>
</html>
Save the above code in a file, JSON-stringify-Object.html, and open it in a Web browser. You see the following output:
String from JSON.stringify():
String object = "false"
Boolean object = false
Date object = "2006-01-02T20:04:05.000Z"
Object object = {"name":"Joe","type":"User"}
JSON object = {}
Function object = undefined
⇒ Call JSON.stringify() with Replacer Function
2017-09-08, ∼2948🔥, 0💬
Popular Posts:
How To Create an Array with a Sequence of Integers or Characters in PHP? The quickest way to create ...
How To Pad an Array with the Same Value Multiple Times in PHP? If you want to add the same value mul...
What is EPUB 2.0 Metadata "dc:creator" and "dc:contributor" elements? EPUB 2.0 Metadata "dc:creator"...
What properties and functions are supported on http.client.HTTPResponse objects? If you get an http....
How to build a test service operation to dump everything from the "context.Request" object in the re...