Home >Web Front-end >JS Tutorial >How to traverse json array with JQuery
JQuery method of traversing json array: 1. Use each traversal, the code is [$.each(obj, function (n, value) {alert(n '']]; 2. jquery traverses and parses json objects 1 , the code is [for(var key in].
The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.
Recommendation: jquery video tutorial
JQuery method of traversing json array:
1. Use each traversal
$(function () { var tbody = ""; //------------遍历对象 .each的使用------------- //对象语法JSON数据格式(当服务器端回调回来的对象数据格式是json数据格式,必须保证JSON的格式要求,回调的对象必须使用eval函数进行转化(否则将得不到Object)。本文不作详细介绍服务器端回调的数据问题,我们将直接自定义对象) var obj = [{ "name": "项海军", "password": "123456"}]; $("#result").html("------------遍历对象 .each的使用-------------"); alert(obj); //是个object元素 //下面使用each进行遍历 $.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); });
2. jquery traversal and parsing json object 1:
var json = [{dd:'SB',AA:'东东',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]); } }
3. jquery traversal and parsing json object 2
There are the following json objects:
var obj ={”name”:”冯娟”,”password”:”123456″,”department”:”技术部”,”sex”:” 女”,”old”:30}; 遍历方法: for(var p in obj){ str = str+obj[p]+','; return str; }
Related free learning recommendations: js video tutorial
The above is the detailed content of How to traverse json array with JQuery. For more information, please follow other related articles on the PHP Chinese website!