Analysis of C/C ++, Java, PHP, JavaScript, Json array, object Value assignment can be followed by a comma, javascriptjson

Source: Internet
Author: User

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

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.