JQuery traverses the json method analysis, and jquery traverses the json
This article analyzes jQuery's method of traversing json. We will share this with you for your reference. The details are as follows:
Copy codeThe Code is as follows: var obj = {"status": 1, "bkmsg": "\ u6210 \ u529f", "bkdata": ["\ u5415 \ u5c1a \ u5fd7 ", "1387580400", "\ u6dfb \ u52a0 \ u8bb0 \ u5f55"]} {"status": 1, "bkmsg": "\ u6210 \ u529f", "bkdata ": ["\ u5415 \ u5c1a \ u5fd7", "1387580400", "\ u6dfb \ u52a0 \ u8bb0 \ u5f55"]}, {"status": 1, "bkmsg ": "\ u6210 \ u529f", "bkdata": ["\ u5415 \ u5c1a \ u5fd7", "1387580400", "\ u4ec0 \ u4e48 \ u4e5f \ u6ca1 \ u6709"]}
Ajax request:
$. Ajax ({url: '/path/to/file', type: 'get', dataType: 'json', data: {param1: 'value1'}, success: function (obj) {// traverse obj }})
The returned content is in the success function. All traversal operations are performed here:
For Loop:
Var obj = {"status": 1, "bkmsg": "\ u6210 \ u529f", "bkdata": ["\ u5415 \ u5c1a \ u5fd7", "1387580400 ", "\ u6dfb \ u52a0 \ u8bb0 \ u5f55"]} // console. log (obj. length); if (obj. status = 1) {for (var I = 0; I <obj. bkdata. length; I ++) {console. log (obj. bkdata [I]) ;};} else {alert ("data error ~ ");};
For in loop:
// For in loop for (x in obj. bkdata) {// x indicates a subscript to specify a variable. The specified variable can be an array element or an object attribute. Console. log (obj. bkdata [x]);} // element each method if (obj. status = 1) {$ (obj. bkdata ). each (function (index, item) {// index indicates the subscript // item indicates the content of the corresponding element. // this indicates each element object // console. log (obj. bkdata [index]); console. log (item); // console. log ($ (this) ;}) ;}else {alert ("data error ~ ") ;}; // Jquery each method $. each (obj. bkdata, function (index, item) {console. log (item );});