Home > Web Front-end > JS Tutorial > body text

Do json have to use curly brackets?

(*-*)浩
Release: 2020-09-18 13:42:31
Original
3086 people have browsed it

json must use braces, because the syntax of json requires that it must be wrapped in braces, and the data is in [KEY: vlaue] mode, and any supported type can be represented by JSON, such as " String, number, object", etc., but objects and arrays are two special and commonly used types, where objects are represented as key-value pairs and data are separated by commas.

Do json have to use curly brackets?

JSON syntax rules

In the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, etc. However, objects and arrays are two special and commonly used types:

Objects are represented as key-value pairs

Data is separated by commas

Curly braces save objects

Square brackets save arrays

JSON key/value pairs

JSON key-value pairs are a way to save JS objects, and they are also written in the same way as JS objects. Much the same, the key name in the key/value pair combination is written in front and wrapped in double quotes "", separated by colon:, and then followed by the value:

{"firstName": "Json"}
Copy after login

This is easy to understand and is equivalent to this JavaScript statement .

{firstName : "Json"}
Copy after login

1. { } curly brackets indicate the definition of an object. In most cases, it must have paired attributes and values, or functions.

For example: var LangShen = {"Name":"Langshen","AGE":"28"};

The above declares an object named "LangShen", Multiple properties or functions are separated by, (comma), because they are properties of the object,

, so when accessing, you should use . (dot) to access layer by layer: LangShen.Name, LangShen.AGE, of course we You can also use arrays to access, such as: LangShen["Name"], LangShen["AGE"], the result is the same.

This writing method is often used in JSON data structure. In addition, when we usually write function groups, it is also often used, such as:

var LangShen = {
      Name = function(){
                 return "LangShen";
                  },
     Age = function(){
                return "28";
                }
}
Copy after login

The calling method is similar, because it is Function group, so add (), such as: alert( LangShen.Name() );

2. [ ] brackets represent 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 only separated by, (comma), because it is an array object, so it is equal to:

var LangShen = Array( "Name","LangShen","AGE","28" );
Copy after login

When accessed, it is the same as the array, alert(LangShen[0]);

3. { } and [ ] are used together. As we mentioned earlier, { } is an object and [ ] is an array. We can form an object array, such as:

var LangShen = { "Name":"Langshen",
    "MyWife":[ "LuLu","26" ],
    "MySon":[{"Name":"Son1"},{"Name":"Son2"},{"Name":"Son3"}] 
}
Copy after login

From the above From a structural point of view, the first item in an object is an attribute, the second item is an array, and the third item is an array containing multiple objects. When called, it is also accessed layer by layer. The properties of the object are superimposed with . (dot), and the array is accessed with [subscript].

The above is the detailed content of Do json have to use curly brackets?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template