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!