Let's talk about the use of {} braces and [] brackets in JS. After understanding them, we can understand the JSON structure.

Source: Internet
Author: User
Let's talk about the use of {} braces and [] brackets in JS. After understanding them, we can understand the JSON structure.

1. braces {} indicate defining an object. In most cases, there must be a pair of attributes and values or functions.

For example, VAR langshen = {"name": "langshen", "Age": "28 "};

The above declares an object named "langshen". multiple attributes or functions are separated by commas (,) because they are attributes of objects,

Therefore, you should use. (point) layer-by-layer access: langshen. name, langshen. age, of course, we can also use Arrays for access, such as langshen ["name"] And langshen ["Age"]. The results are the same.

This method is often used in JSON data structures. In addition, we often use it when writing function groups, such:

VaR langshen = {
Name = function (){
Return "langshen ";
},

Age = function (){
Return "28 ";
}

}

The call method is similar. Because it is a function group, add (), for example, alert (langshen. Name ());

2. [] brackets indicate an array, which can also be understood as an array object.

For example, VAR langshen = ["name", "langshen", "Age", "28"];

Obviously, each value or function is independent. Multiple values are separated by commas (,). Because they are array objects, they are equal:

VaR langshen = array ("name", "langshen", "Age", "28 ");

Alert (langshen [0]);

3. {} and [] are used together. As mentioned above, {} is an object and [] is an array. We can form an array of objects, for example:

VaR langshen = {"name": "langshen ",
"Mywife": ["Lulu", "26"],
"Myson": [{"name": "son1" },{ "name": "son2" },{ "name": "son3"}]
}

From the above structure, the first attribute in an object is an attribute, the second is an array, and the third is an array containing multiple objects. Calling is also a layer-by-layer access. Object Attributes are superimposed with. (points), and arrays are accessed with [subscript.

For example, alert (langshen. myson [1]. Name );

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.