Home > Web Front-end > JS Tutorial > Is json a subset of javascript?

Is json a subset of javascript?

醉折花枝作酒筹
Release: 2023-01-07 11:43:41
Original
1945 people have browsed it

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.

Is json a subset of javascript?

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"

3. JSON value

  • Number (integer or floating point number)

  • String (in double quotes)

  • Logical value (true or false)

  • Array (in square brackets)

  • Object (In curly brackets)

  • null

4. JSON object

JSON object is written in curly brackets:

The object can contain multiple name/value pairs:

{ "name" : "a" , "age" : 34}

5. JSON array

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"}
]
}
Copy after login

In the above example, the object "employees" is an array containing three objects . Each object represents a record about a person.

6. JSON uses JavaScript syntax

Example

var  employees = [
{"name"  : "a"  ,  "sex"  :  "nv"}
{"name"  : "b"  ,  "sex"  :  "nan"}
{"name"  : "c"  ,  "sex"  :  "nv"}
];
Copy after login

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";
Copy after login

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!

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