What does JSON mean? Conversion between JSON and JS objects

The full English name of JSON is JavaScript Object Notation, which is "JavaScript Object Notation". Simply put, JSON is the string representation of Javascript objects and arrays. It uses text to represent the information of a JS object or array. Therefore, the essence of JSON is a string.

Function: JSON is a lightweight text data exchange format, similar to XML in function, specially used for storing and transmitting data, but JSON is smaller, faster and easier to parse than XML.

Status: JSON is a data format that was promoted and used in 2001. So far, JSON has become the mainstream data exchange format.

Two structures of JSON

JSON is the use of strings to represent Javascript objects and arrays. Therefore, JSON contains two structures, object and array. Through the mutual nesting of these two structures, various complex data structures can be represented.

Object structure:

The object structure is expressed as content enclosed by { } in JSON. The data structure is a key-value pair structure of { key: value, key: value, … }. Among them, the key must be a string wrapped in English double quotes, and the data type of value can be number, string, Boolean, null, array, and object.

{
    
    
    name: "zs",
    'age': 20,
    "gender": '男',
    "address": undefined,
    "hobby": ["吃饭", "睡觉", '打豆豆']
    say: function() {
    
    }
}
{
    
    
    "name": "zs",
    "age": 20,
    "gender": "男",
    "address": null,
    "hobby": ["吃饭", "睡觉", "打豆豆"]
}

Array structure: The array structure is expressed as the content enclosed by [ ] in JSON. The data structure is [ "java", "javascript", 30, true ... ] . The type of data in the array can be number, string, Boolean, null, array, object 6 types.

[ "java", "python", "php" ]
[ 100, 200, 300.5 ]
[ true, false, null ]
[ {
    
     "name": "zs", "age": 20}, {
    
     "name": "ls", "age": 30} ]
[ [ "苹果", "榴莲", "椰子" ], [ 4, 50, 5 ] ]

Notes on JSON syntax:

Attribute names must be wrapped in double quotes, and values ​​of string type must be wrapped in double quotes.

Single quotes are not allowed to represent strings in JSON, and comments cannot be written in JSON.

The outermost layer of JSON must be in object or array format, and undefined or functions cannot be used as JSON values.

What JSON does: Store and transfer data between computers and the web.

The essence of JSON: use strings to represent Javascript object data or array data

To convert from a JSON string to a JS object, use the JSON.parse() method:

var obj = JSON.parse('{"a": "Hello", "b": "World"}')
//结果是 {
    
    a: 'Hello', b: 'World'}

To convert from a JS object to a JSON string, use the JSON.stringify() method:

var json = JSON.stringify({
    
    a: 'Hello', b: 'World'})
//结果是 '{"a": "Hello", "b": "World"}'

Guess you like

Origin blog.csdn.net/cz_00001/article/details/131249885