Analysis of C/C ++, Java, PHP, JavaScript, Json array, object Value assignment can be followed by a comma, javascriptjson
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.
I will introduce so much knowledge about whether commas (,) can be followed by the last element in C/C ++, Java, PHP, JavaScript, Json array, and object assignment, I hope it will help you. For more information, please log on to the official website of for details!
Articles you may be interested in:
- Example of a json array depth assignment using javascript