Advantages of json (vs. xml)
1. Readability (readability)
2. Scalability (yes)
3. Coding difficulty (fast)
4. Decoding difficulty (easy)
5. Effective data rate (effective)
Data format written in JSON
Object form:
Object: The object is represented in js as the content enclosed by "{ }", and the data structure is the structure of the key-value pair of {key: value, key: value, ...}. In object-oriented languages, the key is the object The attribute value is the corresponding attribute value, so it is easy to understand. The value method is object.key to get the attribute value. The type of this attribute value can be numbers, strings, arrays, and objects. An object is an unordered collection of "'name/value' pairs".
(1) An object starts with "{" (left bracket) and ends with "}" (right bracket).
(2) Each "name" is followed by a ":" ( English colon );
(3) Use "," ( English comma ) between "'name/value' pairs".
{ "name": "zhangsan", "password": "123456", "email": "[email protected]" }
json data in array form
Array: Array is the content enclosed in square brackets "[ ]" in js, the data structure is [" java ","javascript","vb",...], the value method is the same as in all languages, using index Get, the type of field value can be number, string, array, object. An array is an ordered collection of values.
(1) An array starts with "[" (left bracket) and ends with "]" (right bracket).
(2) Use "," (comma) to separate the values.
/** * If you use var variable declaration, then the calling property is: data.user, you can traverse
* If you do not use var declaration, you cannot perform data manipulation
* If it is local json data requested by ajax, the json file does not need var The variable is declared */ var data = { "user" : [{ "name": "Zhang San" , "password": "1234" }, { "name": "张三", "password": "1234" }, { "name": "张三", "password": "1234" } ] }
Conversion of JSON data and String data
Convert a JSON object to String
JSON.stringify(JSON对象)
Convert a String object in json format to json
The first: eval('('+String object+')')
The second: first introduce the json2.js download address
JSON.parse(String object)
window.onload = function() { var user = { "name": "张三1", "password": "1234" }; console.log(user.name); /** * json object convert string object */ var userStr = JSON.stringify(user); console.log(userStr); /** * string object to json object */ var userJson = eval('(' + userStr + ')'); console.log(userJson.name); /** * Use json2.js to convert json */ var UserJson2 = JSON.parse(userStr); console.log(UserJson2.name); }