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:
We use the JSON.stringify () method for processing the above data, convert it to a string:
myJSON string.
We can myJSON sent to the server:
Examples
try it"
JavaScript array conversion
We can also convert a JavaScript array into a JSON string:
Examples
myJSON string.
We can myJSON sent to the server:
Examples
try it"
abnormal
Analytical data
JSON can not store a Date object.
JSON.stringify () considers all date to a string.
Examples
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
try it"
We can perform JSON.stringify () function to convert the former to a string function to avoid the above problems:
Examples
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