JSON code writing Specification

Source: Internet
Author: User
When AJAX implements frontend and backend data interaction, it usually uses the JSON data format. For JSON, there are strict code specifications. Once the format is incorrect, the corresponding results cannot be displayed, at the same time, an error is not reported on the console. What are the specifications of JSON writing. When AJAX implements frontend and backend data interaction, it usually uses the JSON data format. For JSON, there are strict code specifications. Once the format is incorrect, the corresponding results cannot be displayed, at the same time, an error is not reported on the console. What are the specifications of JSON writing.

What is JSON?

In the interaction between the frontend and backend, messages are usually transmitted to each other, which requires a language that can be understood by both parties. The data format here represents the language. JSON is a "language" that can be understood in the background and background ".

JSON type

JSON also has different organizational forms. One is a JSON object and the other is a JSON array. Therefore, you must follow the basic object and array writing methods in writing code.

1. array Mode

[{            "city" : "BeiJing",            "num" : 5        }, {            "city" : "ShenZhen",            "num" : 5        }, {            "city" : "XiaMen",            "num" : 5        }]

2. Object Method

{            "user" : "ZhangSan",            "type" : "work",            "team" : [{                "city" : "BeiJing",                "num" : 3            }, {                "city" : "GuangZhou",                "num" : 3            }, {                "city" : "ShangHai",                "num" : 3            }]        }

Notes for writing JSON

1. Strings in an array or object must use double quotation marks. single quotation marks are not allowed.

{'User': 'zhangsan'} // invalid
{"User": 'hangsan'} // invalid

2. The object member name must use double quotation marks

{"User": "zhangsan"} // valid

3. The last member of an array or object cannot be followed by a comma

[{"City": "BeiJing", "num": 5, // invalid}, {"city": "ShenZhen", "num": 5, // invalid}]

4. The values of each member of an array or object can be simple values or composite values. Simple values can be divided into four types: String, value (which must be expressed in decimal format), Boolean value, and null (NaN, Infinity,-Infinity, and undefined are both converted to null ). There are two types of composite values: Objects in JSON format and arrays in JSON format.

{"Age": ox16} // invalid. The value must be in decimal format.
{"City": undefined} // use undefined, invalid

1 {"city": null,

2 "getcity": function (){

3 console. log ("error usage ");

4 }}// custom functions or built-in system functions (such as Date () cannot be used in JSON ())



For more articles on JSON code writing specifications, refer to the PHP Chinese website!

Related Article

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.