When C/C ++, Java, PHP, JavaScript, Json arrays, and objects are assigned values, can the last element be followed by a comma?
1 C, C ++, Java, and PHP can tolerate commas at the end
When values are assigned to arrays in C, C ++, and Java, the comma at the end of the last element is dispensable. The following two lines of code are equivalent to these languages.
Int a [] = {1, 2, 3};/* correct */int a [] = {1, 2, 3,};/* correct */
PHP also inherits the characteristics of C. The following two lines of code are equivalent.
$ A = array (1, 2, 3);/* correct */$ a = array (1, 2, 3,);/* correct */
2. JavaScript regards the end Of the comma as a syntax error!
However, when JavaScript is used, there may be no comma at the end of the last element. Otherwise, a syntax error occurs.
Var a = new Array (, 3); // correct var a = new Array (, 3,); // Error
Objects cannot have a comma at the end.
Var o = {name: 'zhao ', age: 12}; // correct var o = {name: 'zhao', age: 12,}; // Error
Although some browsers have the maximum tolerance to execute this error after detecting it, this is not a unified behavior. All browsers of the IE series cannot tolerate such errors.
3 JSON cannot tolerate the end comma.
{"Name": "zhao", "age": 12} // The correct JSON format {"name": "zhao", "age": 12 ,} // incorrect JSON format
It should be noted that JSON is a common data format and has nothing to do with a specific programming language. Different languages also adopt different levels of tolerance when decoding JSON. PHP json_decode () cannot tolerate the end comma.
Json_decode ({"name": "zhao", "age": 12,}); // an error occurs during parsing.