Home > Web Front-end > JS Tutorial > body text

How to traverse json array

(*-*)浩
Release: 2020-09-19 11:23:59
Original
6474 people have browsed it

Methods to traverse the json array: 1. Use each to traverse the json array, with statements such as "$.each(obj,function(n,value){...}"; 2. Use for to traverse and parse json, Statements such as "for(var p in obj){...}".

How to traverse json array

This article mainly introduces two methods of JQuery traversing json arrays. This article gives the methods of using each and for to traverse json respectively. Friends who need it can refer to the following

1. Using each to traverse

$(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);
       });
Copy after login

2. Using for traverses and parses json:

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]);
    }
 }
Copy after login

The above is the detailed content of How to traverse json array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template