Friday, May 11, 2018

Exception of JSON Stringify Date

In JSON, date objects are not allowed. The JSON.stringify() function will convert any dates into strings.

ex.
var obj = {"name"="John", "today"= new Date(), "city"="Newe York"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").InnerHTML = myJSON.

exception 2
In JSON, functions are not allowed as object values.
The JSON.stringify() function will remove any functions from a JavaScript oject, boht key and the value:

ex: var obj = {"name"="John","age": function() {return 30;}, "city":"New York"};
var myJSON = JSON.stringify ( obj );
document.getElementById("demo").innerHTML = myJSON;

output ===> {"name":"John","city":"New York"}

This can be omitted if you convert your functions into strings before running the JSON.stringify() function. 


ex:
var obj = {"name"="John", "age"=function(){return 30;},"city":"New York"}
obj.age = obj.age.ToString();
var myJSON = JSON.stringify(obj);
document.getElementById("demo") = myJSON
outpout ==> {"name":"John","age":"function () {return 30;}","city":"New York"}


No comments:

Post a Comment