The JSON object of JavaScript

Source: Internet
Author: User

JSON is the abbreviation for JAVASCRI object Notatio (JavaScript Object notation). It is a lightweight data interchange format.

JSON is a strict subset of JavaScript and is a JavaScript-based object literal representation that represents structured data.

1.JSON syntax

Support for three types of data, note: variables, functions, object instances are not supported.

1. Simple values: You can represent strings, numbers, booleans, and null. Note that undefined is not supported. The string must use double quotation marks.

2. Object: Represents an unordered key-value pair. object has no variables and semicolons. and the property value of the object must be double-quoted.

Example

{"name": "Tian" "age": +,"school": "Xidian"}

3. Array: Ordered list of values. The array has no variables and semicolons.

[{"title": "Hello"}, "name", +,true

Ii. Methods of JSON objects

1.stringify (): Serializes a JSON object into a JSON string

The method receives three parameters;

A. The JSON object to serialize must be

B. Specify how serialization is to be serialized. is a filter. Can be an array, or it can be a function (a filter function)

If an array, the attributes listed in the array are filtered.

For a function, the function receives two parameters, the property name key, and the value of the property. The result of the serialization is determined based on the return value of the function. Each property of the object goes through the filter.

If the return value is undefined, the corresponding property is ignored.

C. Indicates whether indentation is preserved in the JSON string

If it is a number, the number of characters is indented, and the result contains a newline character.

If it is a string, it is used as the indent character.

D. Because the Json.stringify () method sometimes does not meet the requirements of some object serialization. In some cases, the Tojson method is added to the object, returning its own JSON data format.

ToJSON () can be added as a function filter.

Summary: The order of the serialized objects for Json.stringify () is as follows:

    • This method is called if the JavaScript object has a Tojson () method and the method can get a valid value. Otherwise, the JavaScript object object itself is returned.
    • If there is a second argument, apply this function filter. The value of the incoming filter is the value returned in the first step.
    • Each value returned in the second step is serialized accordingly.
    • If there is a third argument, the corresponding formatting is performed.

2.parse (): Parses a JavaScript string into a JSON object

The method receives three parameters;

A. The JavaScript string to parse, which must be

B. Restore function, optional. Should be used on each key-value pair.

The JSON object of JavaScript

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.