Json.stringify ()
JSON is typically used to exchange data with the server.
is typically a string when sending data to the server.
We can use the Json.stringify () method to convert a JavaScript object to a string.
Grammar
Json.stringify (value[, replacer[, space])
Parameter description:
- Value
Required, a valid JSON string.
- Replacer:
Optional. The function or array used to convert the result.
If Replacer is a function, Json.stringify calls the function and passes in the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string: "".
If Replacer is an array, only the members with key values in the array are converted. The members are converted in the same order as the keys in the array. When the value parameter is also an array, replacer arrays are ignored.
- Space
Optionally, text adds indents, spaces, and line breaks, and if space is a number, the return value text indents the specified number of spaces at each level, and if space is greater than 10, the text indents 10 spaces. Space has the ability to use non-numeric, such as: \ T.
JavaScript Object Conversions
For example, we send the following data to the server:
var obj = {"Name": "Runoob", "Alexa": 10000, "site": "Www.runoob.com"};
We use the Json.stringify () method to process the above data and convert it to a string:
var Myjson = json.stringify (obj);
Myjson is a string.
We can send the Myjson to the server:
var obj = {"Name": "Runoob", "Alexa": 10000, "site": "www.runoob.com"};var Myjson = json.stringify (obj); document.getElementById ("Demo"). InnerHTML = Myjson;
JavaScript Array Conversion
We can also convert the JavaScript array to a JSON string:
var arr = ["Google", "Runoob", "Taobao", "Facebook"];var Myjson = json.stringify (arr);
Myjson is a string.
We can send the Myjson to the server:
var arr = ["Google", "Runoob", "Taobao", "Facebook"];var Myjson = json.stringify (arr);d Ocument.getelementbyid ("demo"). I nnerhtml = Myjson;
Exception parsing data
JSON cannot store Date objects.
Json.stringify () converts all dates to strings.
var obj = {"Name": "Runoob", "initdate": New Date (), "site": "www.runoob.com"};var Myjson = json.stringify (obj); document.getElementById ("Demo"). InnerHTML = Myjson;
Results:
Analytic functions
JSON does not allow the inclusion of functions, json.stringify () removes functions of JavaScript objects, including key and value.
var obj = {"Name": "Runoob", "Alexa": function () {return 10000;}, "Site": "www.runoob.com"};var Myjson = json.stringify (obj ); document.getElementById ("Demo"). InnerHTML = Myjson;
Results:
We can avoid this problem by converting the function to a string before executing the json.stringify () function:
var obj = {"Name": "Runoob", "Alexa": function () {return 10000;}, "Site": "www.runoob.com"};obj.alexa = obj.alexa.toString (); var Myjson = json.stringify (obj); document.getElementById ("Demo"). InnerHTML = Myjson;
Results:
Json.stringify ()--js to JSON