JavaScript JSON

JSON is the abbreviation of JavaScript Object Notation, which is a data exchange format.

What is JSON?

The full English name of JSON is JavaScript Object Notation

JSON is a lightweight data exchange format.

JSON is an independent language *

JSON is easy to understand.


In JSON, there are only a few data types:

number: exactly the same as JavaScript’s number ;

boolean: is JavaScript’s true or false;

string: is JavaScript’s string;

null: It is the null of JavaScript;

array: It is the Array representation of JavaScript - [];

object: It is JavaScript's { ... } representation.

and any combination of the above.

Moreover, JSON also stipulates that the character set must be UTF-8, so there is no problem in expressing multiple languages. For unified parsing, JSON strings must use double quotes "", and Object keys must also use double quotes "".

Because JSON is very simple, it quickly became popular in the Web world and became an ECMA standard. Almost all programming languages have libraries for parsing JSON, and in JavaScript, we can use JSON directly because JavaScript has built-in JSON parsing.

    php中文网(php.cn)  


JSON is formatted into a JavaScript object

The JSON format is syntactically identical to creating a JavaScript object The code is the same.

Because they are similar, JavaScript programs can easily convert JSON data into JavaScript objects.


JSON syntax rules

Data is key/value pairs. Data is separated by commas. Curly brackets save the object Square brackets save the array


##JSON data - one name corresponds to one value

JSON The data format is key/value pairs, just like JavaScript object properties.

A key/value pair consists of the field name (in double quotes), followed by a colon, and then the value:

"firstName":"John"


JSON Object

JSON objects are stored within curly braces.

Just like in JavaScript, objects can hold multiple key/value pairs:

{"firstName":"John", "lastName":"Doe"}


JSON array

JSON array is stored in square brackets.

Just like in JavaScript, arrays can contain objects:

"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith "},
{"firstName":"Peter", "lastName":"Jones"}
]

In the above example, the object "employees" is an array. Contains three objects.

Each object is a record of an employee (last name and first name).


Convert JSON string to JavaScript object

Usually we read JSON data from the server, and display the data in the web page.

First, create a JavaScript string, which is data in JSON format:

var text = '{ "employees " : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" } ,' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';

Then, use the JavaScript built-in function JSON.parse() to parse the characters Convert the string to a JavaScript object:

var obj = JSON.parse(text);

Finally, use the new JavaScript object in your page:

    php中文网(php.cn) 



Continuing Learning
||
php中文网(php.cn)
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!