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

Is json a subset of javascript?

Robert De Niro
Robert De NiroOriginal
2021-07-16 09:42:131833browse

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"}
]
}

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"}
];

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!

Statement:
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