Json.stringify ()--js to JSON

Source: Internet
Author: User
Tags javascript array

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.