What is JSON:is a data interchange format that can replace XML JSON syntax rules:in the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, and so on. However, objects and arrays are two of the more special and common types: 1. The object is represented as a key-value pair of 2. The data is separated by commas by 3. Curly braces Save Object 4. Square brackets Save ArrayJSON key/value pairs:The JSON key-value pair is a way to save the JS object, and the syntax of the JS object is similar, the key/value pairs in the combination of key names are written in front and double quotation marks "" package, using a colon: delimited, and then immediately after the valuethe relationship between JSON and JS objects: JSON is a string representation of a JS object, which uses text to represent the information of a JS object, essentially a stringJSON and JS objects are transferred to each other: To implement a conversion from an object to a JSON string, use the Json.stringify () method:
1 |
var json = JSON.stringify({a: ‘Hello‘ , b: ‘World‘ }); //结果是 ‘{"a": "Hello", "b": "World"}‘ |
To implement a conversion from JSON to an object, use the Json.parse () method:
1 |
var obj = JSON.parse( ‘{"a": "Hello", "b": "World"}‘ ); //结果是 {a: ‘Hello‘, b: ‘World‘} |
Common types: In the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, and so on. However, objects and arrays are two of the more special and common types. Object: Object in JS is the contents of the curly brace wrapped {}, the data structure is {key1:value1, key2:value2, ...} key-value pair structure. In an object-oriented language, key is the property of the object, and value is the corresponding values. Key names can be represented using integers and strings. The type of the value can be any type. Array: The array in JS is square brackets [] wrapped content, data structure for ["Java", "JavaScript", "VB", ...] index structure. In JS, an array is a special type of data, and it can also use key-value pairs like objects, but it is still much used by indexes. Similarly, the type of the value can be any type.
JSON (JavaScript Object Notation)