In the JSON.stringify JS () method, the object js (JSON string) into a string, the incoming server

Original link: http://www.cnblogs.com/mark5/p/10955223.html

JSON typically used to exchange data with the server.

When sending data to the server is typically a string.

We can use JSON.stringify () method to convert a string JavaScript object.

grammar

JSON.stringify(value[, replacer[, space]])

Parameter Description:

  • value:

    Necessary, to convert JavaScript value (usually object or array).

  • replacer:

    Optional. Or a function for converting the result array.

    If replacer is a function, then JSON.stringify will call the function and pass keys and values ​​each member. Use the return value instead of the original value. If this function returns undefined, the exclusion of members. Root object key is an empty string: "."

    If replacer is an array, the array conversion only members with key values. Conversion order with key members of the same order in the array. When the value parameter is also an array, the array will be ignored replacer.

  • space:

    Alternatively, add text indent, spaces and line breaks, if space is a number, the value of a specified number of text indent level for each space, if the space is larger than 10, the space 10 to return indented text. non-numerical space may be used, such as: \ t.


JavaScript Object Conversion

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 for processing the above data, convert it to a string:

var myJSON = JSON.stringify(obj);

myJSON string.

We can myJSON sent to the server:

Examples

var obj = { "name":"runoob", "alexa":10000, "site":"www.runoob.com"}; var myJSON = JSON.stringify(obj); document.getElementById("demo").innerHTML = myJSON;

try it"

JavaScript array conversion

We can also convert a JavaScript array into a JSON string:

Examples

var arr = [ "Google", "Runoob", "Taobao", "Facebook" ]; var myJSON = JSON.stringify(arr);

myJSON string.

We can myJSON sent to the server:

Examples

var arr = [ "Google", "Runoob", "Taobao", "Facebook" ]; var myJSON = JSON.stringify(arr); document.getElementById("demo").innerHTML = myJSON;

try it"

abnormal

Analytical data

JSON can not store a Date object.

JSON.stringify () considers all date to a string.

Examples

var obj = { "name":"Runoob", "initDate":new Date(), "site":"www.runoob.com"}; var myJSON = JSON.stringify(obj); document.getElementById("demo").innerHTML = myJSON;

try it"

Then you can then convert a string to a Date object.


Analytic Functions

JSON not allowed function, JSON.stringify () JavaScript function deletes the objects, including the key and value.

Examples

var obj = { "name":"Runoob", "alexa":function () {return 10000;}, "site":"www.runoob.com"}; var myJSON = JSON.stringify(obj); document.getElementById("demo").innerHTML = myJSON;

try it"

We can perform JSON.stringify () function to convert the former to a string function to avoid the above problems:

Examples

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;

try it"

Not recommended function in JSON.


Browser support

Major browsers support JSON.stringify () function:

    • Firefox 3.5
    • Internet Explorer 8
    • Chrome
    • Opera 10
    • Safari 4

Reproduced in: https: //www.cnblogs.com/mark5/p/10955223.html

Guess you like

Origin blog.csdn.net/weixin_30707875/article/details/94878750