Home >Web Front-end >JS Tutorial >Understand the relevant syntax of json
The relevant syntax of json is very important to js, and this article will explain it.
JSON syntax rules
JSON syntax is a subset of JavaScript object notation syntax.
Data is in name/value pairs
Data is separated by commas
Curly braces hold objects
Square brackets hold arrays
JSON Name/value pair
The writing format of JSON data is: name/value pair.
A name/value pair consists of the field name (in double quotes), followed by a colon, and then the value:
"firstName" : "John"
This is easy to understand, equivalent to this JavaScript statement:
firstName = "John"
JSON value
JSON value can be:
Number ( Integer or floating point number)
String (in double quotes)
Logical value (true or false)
Array (in square brackets)
Object (in curly brackets)
null
JSON object
JSON object in flower Written in brackets:
Objects can contain multiple name/value pairs:
{ "firstName":"John" , "lastName":"Doe" }
This is also easy to understand and is equivalent to this JavaScript statement:
firstName = " John"
lastName = "Doe"
JSON array
JSON array is written in square brackets:
The array can contain multiple objects:
{ "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] }
In the above example, the object "employees" is an array containing three objects. Each object represents a record about a person (with a first and last name).
JSON uses JavaScript syntax
Because JSON uses JavaScript syntax, no additional software is required to process JSON in JavaScript.
With JavaScript, you can Create an array of objects and assign values like this:
Example
var employees = [ { "firstName":"Bill" , "lastName":"Gates" }, { "firstName":"George" , "lastName":"Bush" }, { "firstName":"Thomas" , "lastName": "Carter" } ];
You can access JavaScript object arrays like this The first item:
employees[0].lastName;
The returned content is:
Gates
can be like Modify the data like this:
employees[0].lastName = "Jobs";
In the following chapters, you will learn how to convert JSON text into JavaScript objects.
JSON file
The file type of the JSON file is ".json"
The MIME type of the JSON text is "application/json"
This article is correct json has made relevant explanations. For more learning materials, please pay attention to the php Chinese website to view.
Related recommendations:
##Related understanding of PHP filters
About PHP exception handling operations
The above is the detailed content of Understand the relevant syntax of json. For more information, please follow other related articles on the PHP Chinese website!