Home  >  Article  >  Web Front-end  >  How to traverse json array with JQuery

How to traverse json array with JQuery

coldplay.xixi
coldplay.xixiOriginal
2020-12-23 11:59:573562browse

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].

How to traverse json array with JQuery

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:&#39;SB&#39;,AA:&#39;东东&#39;,re1:123},{cccc:&#39;dd&#39;,lk:&#39;1qw&#39;}];
 for(var i=0,l=json.length;i<l;i++){
    for(var key in json[i]){
        alert(key+&#39;:&#39;+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]+&#39;,&#39;;
    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn