JQuery traverses the json array in three ways: jqueryjson Array

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.