This article mainly introduces jsjquery's method of parsing json and array formats in detail. If you need it, you can refer to it for help, several concepts must be clarified: what are the differences and contact points between arrays, associated arrays, and json?
1. Introduction to concepts
1. Array
Syntax:
ECMAScript v3 specifies the syntax for the direct amount of arrays, which is implemented by JavaScript 1.2 and JScript 3.0. You can put a comma-separated expression list in square brackets to create and initialize an array. The values of these expressions become array elements. For example:
Var a = [1, true, 'abc'];
For more information, see the API.
Ps: must be separated by square brackets.
2. Join an array
1. Syntax:
Var myhash = {"key1": "val1", "key2": "val2"}; // obj
2. var
Myhash = {key1: "val1", key2: "val2"}; // obj-can also
Ps: The json format is almost the same as the json format, but the json format is more strict (the key-value pair must use double quotation marks). However, json can only be used as a standard format, if you want to operate on it, you must convert it to an associated array object (obj ).
2. Simple operation
1. Add a key value to the Hash join Array
// Add a new key newkey with the key value newval
Myhash ["newkey"] = "newval ";
2. Delete existing key values of the Hash associated array
// Delete a key newkey. At the same time, the newval corresponding to the key value disappears.
Delete myhash ["newkey"];
3. traverse the Hash associated array
// Traverse the entire hash Array
For (key in myhash ){
Val = myhash [key];
}
4. Obtain the value
Method 1. myhash. key1
Method 2. myhash. key2
3. json
Format requirements:
{"Key1": "val1", "key2": "val2"}; // strictly follow this format. operations can be performed based on the associated array.
2. Several key points in front-and-back Interaction
1. When the data sent by the server is not a json, but multiple json, you should contact the array and the associated array to assemble the string.
For example, var objs = [{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}];
2. the data from the beginning to the end server to the client is only a string, so in order to allow it to perform necessary operations on it in js, you can use eval () convert to JavaScript executable objects.
Therefore, the $. parseJSON () is limited. If this is the case mentioned in 1 above, you must use eval () for conversion and then use $. each (objs, function (I, o ){...}) operation
Iii. specific instance code
Page code:
The Code is as follows: