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

jQuery method analysis of traversing json_jquery

WBOY
Release: 2016-05-16 15:05:16
Original
1715 people have browsed it

This article analyzes the jQuery method of traversing json through examples. Share it with everyone for your reference, the details are as follows:

Copy code The code is as follows:
var obj = {"status":1,"bkmsg":"u6210u529f", "bkdata":["u5415u5c1au5fd7","1387580400","u6dfbu52a0u8bb0u5f55"]}{"status":1,"bkmsg":"u6210u529f","bkdata":["u5415u5c1au5fd7","1387580400 ","u6dfbu52a0u8bb0u5f55"] },{"status":1,"bkmsg":"u6210u529f","bkdata":["u5415u5c1au5fd7","1387580400","u4ec0u4e48u4e5fu6ca1u6709"]}

ajax request:

$.ajax({
    url: '/path/to/file',
    type: 'GET',
    dataType: 'json',
    data: {param1: 'value1'},
    success: function (obj){
      //遍历obj
    }
})

Copy after login

The returned content is in the success function, and 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("数据有误~");
};

Copy after login

for in loop:

//for in循环
  for(x in obj.bkdata){
    //x表示是下标,来指定变量,指定的变量可以是数组元素,也可以是对象的属性。
    console.log(obj.bkdata[x]);
  }
//元素 each方法
if (obj.status == 1) {
 $(obj.bkdata).each(function(index,item){
  //index指下标
  //item指代对应元素内容
  //this指代每一个元素对象
  //console.log(obj.bkdata[index]);
  console.log(item);
  //console.log($(this));
 });
}else{
 alert("数据有误~");
};
//jquery each方法
$.each( obj.bkdata, function(index,item){
    console.log(item);
});

Copy after login

Readers who are interested in more jQuery-related content can check out the special topics on this site: "Summary of jQuery's method of operating DOM nodes", "Summary of jQuery traversal algorithms and techniques", " JQuery table (table) operation skills summary", "jQuery drag effects and skills summary", "jQuery extension skills summary", "jQuery Summary of common classic special effects", "jQuery animation and special effects usage summary", "jquery selector usage summary" and "jQuery common plug-ins and usage summary

I hope this article will be helpful to everyone in jQuery programming.

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