JSON is introduced

JSON(JavaScript Object Notation) is a lightweight data exchange pattern. It is based on a subset of ECMAScript (base syntax). Data is stored and represented in a text format that is completely independent of the programming language. It uses conventions similar to the C language clan (including C, C++, C#, Java, JavaScript, Perl, Python, etc.).

Features: 1. Concise and clear hierarchy makes JSON an ideal data exchange language 2. Easy to read and write 3. Easy to parse and generate by machine 4. Improve network transmission efficiency

1. JSON data is written in key/value pairs. JSON key-value pairs are a way of storing JS objects, and are written in much the same way as JS objects. Key/value pairs include the field name (in double quotes), followed by a colon, and then the value. JSON values can be strings (in double quotation marks), arrays (in brackets), numbers (integers or floating-point numbers), logical values (true or false), objects (in braces), or NULL. {} at both ends of it, and with the keyboard value pairs fill Spaces between. Most of the data used in JSON is ultimately encapsulated in JSON objects. Key-value pairs they have a colon between them as “key” : “value”. Each key-value pair is separated by commas, so the middle of the JSON looks like this: “key”: “value”, “key”: “value”, “key”: “value”.

{ 
  "first_name" : "Sammy", 
  "last_name" : "Shark", 
  "location" : "Ocean" 
}
Copy the code

(1) The object consists of comma-separated members enclosed in curly braces. The members are string keys and the values described above consist of comma-separated key-value pairs, as in:

{"name": "John Doe", "age": 18, "address": {"country" : "china", "zip-code": "10000"}}
Copy the code

(2) An array is a set of values enclosed in square brackets, such as [3, 1, 4, 1, 5, 9, 2, 6] (3) Strings are very similar to C or Java strings. A string is a collection of any number of Unicode characters surrounded by double quotes, escaped using a backslash. A character is a single character string.

The numbers are also very similar to C or Java values. Remove octal and hexadecimal formats that are not used. Some examples of valid JSON

{" a ": 1," b ": [1, 2, 3]} [1, 2," 3 ", 4} {" a ":] 3.14" plain_text"Copy the code

XML and JSON both use structured ways to tag data, so let’s do a simple comparison. Data of some provinces and cities in China expressed in XML are as follows:

<country> <name> <province> <name> <cities> <city> <city> <cities> <province> <province> <name> <city> <city> <city> <city> <city> <city> <city> <city> <city> <province> <city> <city> <city> <city> <city> <province> <name> Taiwan </name> < City > Taipei </city> <city> Kaohsiung </city> </city> </province> </province> </name> </city> </city> </province> </country>Copy the code

Expressed in JSON as follows:

{" name ":" Chinese ", "province" : [{" name ":" heilongjiang province ", "cities" : {" city ": [" Harbin", "daqing"]}}, {" name ":" guangdong ", "cities" : {" city ": [" guangzhou", "shenzhen", "zhuhai"]}}, {" name ":" Taiwan ", "cities" : {" city ": [" Taipei", "kaohsiung"]}}, {" name ":" xinjiang ", "cities" : {"city": [" city"]}}]}Copy the code

Advantage:

The syntax format is simple. The hierarchical structure is clear and easy to read. The data exchange saves the bandwidth occupied by data transmission