Syntax and Formatting in JSON

1. Two array structures in JSON:

  1. Object structure: The object structure is expressed as the 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.
  2. Array structure: The array structure is expressed as the content enclosed by [] in JSON. The data structure is [ "java", "javascript", 30, true ...]. The data types in the array can be numbers, strings, Boolean values, null, arrays, and objects.

2. Notes on JSON syntax:

  1. Attribute names must be wrapped in double quotes
  2. Values ​​of type string must be wrapped in double quotes
  3. Single quotes for strings are not allowed in JSON
  4. Comments cannot be written in JSON
  5. The outermost layer of JSON must be in object or array format
  6. Cannot use undefined or functions as JSON values

What JSON does: Store and transfer data between computers and networks.

3. The relationship between JSON and js objects

  1. JSON is a string representation of JS objects, which uses text to represent the information of a JS object, essentially a string. For example:

This is an object
var obj = {a:'Hello', b: ' World' }
This is a JSON string, essentially a string
var json = '{"a": "Hello", "b": "World"}'

4. Mutual conversion between JSON and js objects

  1. To convert from a JSON string to a JS object, use the JSON.parse() method:
    var obj = JSON.parse(' {"a": "Hello", "b": "World"} ')

turn out{a: 'Hello', b: 'World'}

  1. To convert from a JS object to a JSON string, use JSON.stringify()the method:
    var json = JSON. stringify({a 'Hello', b: 'World'})

turn out' {"a": "Hello", "b": "World"} '

5. Deserialization and deserialization of JSON

  1. The process of converting data objects into strings is called serialization, for example, the operation of calling the JSON.stringify0 function is called JSON serialization.
  2. The process of converting a string into a data object is called deserialization. For example, the operation of calling the JSON.parse0 function is called JSON deserialization.

Guess you like

Origin blog.csdn.net/m0_63144319/article/details/130297570