jQuery的遍历从阿贾克斯成功JSON结果? [英] jQuery loop over JSON result from AJAX Success?

查看:106
本文介绍了jQuery的遍历从阿贾克斯成功JSON结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery的AJAX成功的回调我要循环对象的结果。这是如何响应看起来在萤火虫一个例子

  [
 {TEST1:45,TEST2:23,TEST3:DATA1},
 {TEST1:46,测试2:24,TEST3:DATA2},
 {TEST1:47,测试2:25,TEST3:DATA3}
]
 

我如何遍历结果,这样我就可以访问每一个元素? 我曾尝试类似下面但这似乎并不奏效。

  jQuery.each(数据,函数(指数的ItemData){
  // itemData.TEST1
  // itemData.TEST2
  // itemData.TEST3
});
 

解决方案

您可以删除外循环,更换数据。数据

  $。每个(数据。数据,功能(K,V){
    ///做的东西
});
 

您已经接近:

  $。每个(数据,函数(){
  $每个(此功能(K,V){
    ///做的东西
  });
});
 

您有对象/图数组所以外循环迭代的。内循环遍历每个对象元素的属​​性。

On the jQuery AJAX success callback I want to loop over the results of the object. This is an example of how the response looks in Firebug.

[
 {"TEST1":45,"TEST2":23,"TEST3":"DATA1"},
 {"TEST1":46,"TEST2":24,"TEST3":"DATA2"},
 {"TEST1":47,"TEST2":25,"TEST3":"DATA3"}
]

How can I loop over the results so that I would have access to each of the elements? I have tried something like below but this does not seem to be working.

jQuery.each(data, function(index, itemData) {
  // itemData.TEST1
  // itemData.TEST2
  // itemData.TEST3
});

解决方案

you can remove the outer loop and replace this with data.data:

$.each(data.data, function(k, v) {
    /// do stuff
});

You were close:

$.each(data, function() {
  $.each(this, function(k, v) {
    /// do stuff
  });
});

You have an array of objects/maps so the outer loop iterates over those. The inner loop iterates over the properties on each object element.

这篇关于jQuery的遍历从阿贾克斯成功JSON结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆