如何在 jQuery 中遍历这个 JSON 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9887009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:25:07  来源:igfitidea点击:

How do I iterate through this JSON object in jQuery?

jqueryjsonobjectloops

提问by Johnathan Au

I have a JSON object which is generated by PHP. It's an object with a set of dates. It has the timeStamp and then a formatted version of the date. How would I iterate through this in jQuery?

我有一个由 PHP 生成的 JSON 对象。它是一个带有一组日期的对象。它有时间戳,然后是日期的格式化版本。我将如何在 jQuery 中迭代这个?

{
  "dates":[
    {
      "timeStamp": 1317596400,
      "formattedDate": "Mon 03 October 2011"
    },
    {
      "timeStamp": 1317682800,
      "formattedDate": "Tue 04 October 2011"
    },
    {
      "timeStamp": 1317855600,
      "formattedDate": "Thu 06 October 2011"
    }
  ]
}

I've tried:

我试过了:

for (var i in data) { 
  alert(data.dates[i].timeStamp); 
};

for (var i in data) { 
  alert(data[i].dates.timeStamp); 
};

and

for (var i in data) { 
  alert(data.dates.timeStamp[i]); 
};

回答by gion_13

Since you tagged your question as a jqueryone, you should use $.eachbecause it's jquery's iterator function:

由于您将问题标记为一个问题jquery,因此您应该使用$.each它,因为它是 jquery 的迭代器函数:

$.each(data.dates, function(index, element) {
    alert(element.timeStamp); 
});

If you want to stick to the for insyntax (which i see you've tried), a solution might be :

如果你想坚持for in语法(我看到你已经尝试过),一个解决方案可能是:

for(var key in data.dates) {
     alert(data.dates[key].timeStamp); 
} 

But beware that the for insyntax may do more than you think it does: it iterates over the properties inherited from the prototype too, so it might be usefull to make sure you iterate only on the object instance properties:

但请注意,该for in语法的作用可能比您想象的要多:它也会迭代从原型继承的属性,因此确保仅对对象实例属性进行迭代可能会很有用:

for(var key in data.dates) {
    // if it's not something from the prototype
    if(data.dates.hasOwnProperty(key)) {
        alert(data.dates[key].timeStamp); 
    }
} 

update
Another elegant way is to use the Object.keysmethod that returns an array containing all the keys in the targeted object to iterate over all the object's properties:

更新
另一种优雅的方法是使用Object.keys返回包含目标对象中所有键的数组的方法来迭代对象的所有属性:

for(var i=0, keys=Object.keys(data.dates), l=keys.length; i<l; i++) {
    alert(data.dates[i].timeStamp);
} 

回答by mfadel

You use $.each().
It looks like this:

你用$.each().
它看起来像这样:

$.each(data, function(n, elem) {
    // here you process your data to data loaded to lines               
});

回答by Mohammed Irfan Tirupattur

You can simply iterate through the json structure using jQuery each:

您可以使用jQuery each简单地遍历 json 结构:

$.each(data, function(index, element) {
   alert(element.dates.timeStamp); 
});

回答by Mike Simmons

jQuery.each() is probably the easiest way, check this out: http://api.jquery.com/jQuery.each/

jQuery.each() 可能是最简单的方法,请查看:http: //api.jquery.com/jQuery.each/

eg

例如

$.each(dates, function(index, date) { alert(date.timeStamp); });