You don't know json.stringify and json.parse.

Source: Internet
Author: User
Tags tojson

JSON is a JavaScript Object notation (JAvaScript Object Notation), which is a simple data format , similar to XML, whose format is a name/value pair, the data is separated by commas, and the name must be enclosed in double quotation marks. For example: {"name": "Wumomo", age:25}

About the points to note:

1. The name must be enclosed in double quotation marks

2. Cannot add comma after last value

3, the value can be a number, a string, an object, an array, a Boolean value (True/false), NULL; cannot be a function, undefined, or Nan

JSON and JavaScript object formats are somewhat similar, but they are also very different and can be transformed from one to the other.

Difference Json JavaScript objects
Meaning A data format Instances of the class
Transmission Can transfer data across platforms with fast speed Cannot transmit
Name/key The name must be enclosed in double quotation marks. Names can be without double quotes
Property value The last value cannot be followed by a comma, and the value cannot be a function/undefined/nan Can be added with a comma, can be any value

JSON to JS object------json.parse (str)

JS object to JSON------json.stringify (obj)

Prior to this project development only contacted and used the Json.parse () and the Json.stringify () method of the first parameter, and then did not go to the drill, in an accidental opportunity to see the other parameters, only to know that the original can also be so.

Json.parse (text[, Reviver])

Text-------required, a string to be parsed into a JavaScript value

Reviver function: function (key,value)------optional, parse the value itself and all the properties it contains, will be in a certain order (from inside to outside) to call the reviver functions separately, if reviver returned undefined , The current property is removed from the owning object, and if other values are returned, the returned value becomes the new property value of the current property. When traversing to the topmost value (parsing value), the parameter of the passed-in reviver function is "" and value is the modified resolved value.

For example:

json.stringify (Value[,replace [, space]])

Value------required and will be serialized into a JSON string

replacer------Optional, filter function or array, if the argument is null or not provided, all properties of the object will be serialized, and when the parameter is an array, only the name of the property contained in the array will be serialized into the final JSON string. The conversion sequence is the same as the order in the array.

Space------Optional, specify the indentation of the blank string for the beautification of the output, if the number represents the number of spaces, if the value is less than 1, it is equal to no space; if it is a string (up to the first 10 characters), take the string instead of a space

For example:

Tojson method

If a serialized object owns a toJSON method, the toJSON method overrides the default serialization behavior of the object

You don't know json.stringify and json.parse.

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.