Before parsing, we must clarify several concepts: What are the differences and contact points between arrays, associative arrays and json?
1. Concept introduction
1. Array
Syntax:
ECMAScript v3 specifies the syntax for array literals, and JavaScript 1.2 and JScript 3.0 implement it. You can create and initialize an array by placing a comma-separated list of expressions in square brackets. The values of these expressions will become array elements. For example:
var a = [1, true, 'abc'];
Check the API for detailed operations.
ps: Must be separated by square brackets.
2. Associative array
1. Grammar:
var myhash= {”key1″:”val1″, “key2″:”val2″ };//obj
2.var
myhash= {key1:”val1″, key2:”val2″};//obj-can also be used
ps: It is almost the same as the json format, but the json format requirements are more strict (the key-value pairs inside must use double quotes), but json can only be used as a format standard. If you want to operate on it, it must be converted into an associative array. Object(obj).
2. Simple operation
1. Add key value to Hash associative array
//Add a new key newkey with the key value newval
myhash["newkey"] = "newval";
2. Delete existing key values in Hash associative array
// Delete a key newkey, and at the same time, the newval corresponding to the key value will disappear
delete myhash[”newkey”];
3. Traverse Hash associative array
// Traverse the entire hash array
for (key in myhash) {
val = myhash[key];
}
4. Obtain value
Method 1.myhash.key1
Method 2.myhash.key2
3.json
Format requirements:
{”key1″:”val1″, “key2″:”val2″};//Strictly follow this format, and the operation can be based on the operation of the associative array
2. Several key points in front-end and back-end interactions
1. When the data sent by the server is not one json, but multiple json, the array should be contacted and associative array to assemble string
For example: var objs = [{ id: 1, name: 'n_1' }, { id: 2, name: 'n_2'}];
2. From beginning to end, the data provided by the server to the client is only a string, so in order to allow it to perform necessary operations on it in js, it can be converted into a js executable object through eval().
Therefore, the $.parseJSON() provided in jQuey has limitations. If it is the situation mentioned in 1 above, you must use eval() for conversion, and then use $.each(objs,function(i ,o){...}) to operate
3. Specific example code
Page code: