Home > Article > Web Front-end > Is json a subset of javascript?
JSON syntax is a subset of JavaScript syntax. JSON is a lightweight data exchange format. It is based on a subset of ECMAScript and uses a text format that is completely independent of programming languages to store and represent data.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JSON syntax is a subset of JavaScript syntax.
1. JSON syntax rules:
JSON syntax is a subset of JavaScript object notation syntax.
(1) Data is in name/value pairs
(2) Data is separated by commas
(3) Curly braces save objects
(4) Square brackets save the array
2. 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:
"name" : "liming" Equivalent to name="liming"
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 is written in curly brackets:
The object can contain multiple name/value pairs:
{ "name" : "a" , "age" : 34}
JSON arrays are written in square brackets:
Arrays can contain multiple objects:
{ “employees” :[ {"name" : "a" , "sex" : "nv"}, {"name" : "b" , "sex" : "nan"}, {"name" : "c" , "sex" : "nv"} ] }
In the above example, the object "employees" is an array containing three objects . Each object represents a record about a person.
var employees = [ {"name" : "a" , "sex" : "nv"} {"name" : "b" , "sex" : "nan"} {"name" : "c" , "sex" : "nv"} ];
You can access the first item in the JavaScript object array like this:
employees[0]. name;
The returned content is: a
Modify data:
employee[0].name= "zhangsan";
Since JSON syntax is a subset of JavaScript syntax, the JavaScript function eval() can be used to convert JSON text is a JavaScript object.
Because the eval() function uses a JavaScript compiler, it can parse JSON text and then generate JavaScript objects. Text must be enclosed in brackets to avoid syntax errors:
For example:
var obj = eval ("(" txt ")");
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of Is json a subset of javascript?. For more information, please follow other related articles on the PHP Chinese website!