JQuery traverses the json array in three ways: jqueryjson Array
1. Use each to traverse
Copy codeThe Code is as follows:
$ (Function (){
Var tbody = "";
// ------------ Use to traverse object. each -------------
// Object syntax JSON data format (when the object data format returned by the server callback is json data format, the JSON format must be guaranteed, the callback Object must be converted using the eval function (otherwise, the Object will not be obtained ). This article does not detail the data issue of server-side callback. We will directly customize the object)
Var obj = [{"name": "", "password": "123456"}];
$ ("# Result" example .html ("------------ use of object. each -------------");
Alert (obj); // It is an object element.
// Use each for traversal below
$. Each (obj, function (n, value ){
Alert (n + ''+ value );
Var trs = "";
Trs + = "<tr> <td>" + value. name + "</td> <td>" + value. password + "</td> </tr> ";
Tbody + = trs;
});
$ ("# Project"). append (tbody );
});
Ii. jquery traverses and parses json object 1:
Copy codeThe Code is as follows:
Var json = [{dd: 'SB ', AA: 'dongdong', re1: 123}, {cccc: 'dd', lk: '1qw'}];
For (var I = 0, l = json. length; I <l; I ++ ){
For (var key in json [I]) {
Alert (key + ':' + json [I] [key]);
}
}
Iii. jquery traverses and parses json object 2
The following json objects are available:
Copy codeThe Code is as follows:
Var obj = {"name": "Feng Juan", "password": "123456", "department": "technology department", "sex": "female", "old ": 30 };
Traversal method:
Copy codeThe Code is as follows:
For (var p in obj ){
Str = str + obj [p] + ',';
Return str;
}