【问题标题】:Properly loop through JSON with jQuery使用 jQuery 正确循环 JSON
【发布时间】:2009-04-06 21:47:16
【问题描述】:

我有以下 JSON:

{
  "status" : "success",
  "0": { 
    "link"  : "test1",
    "img"   : "test2",
    "title" : "test3"
  },
  "1":{
    "link"  : "test4",
    "img"   : "test5",
    "title" : "test6"
  }
}

显然 0 和 1 本身就是对象,我想要一种适当的方法来循环遍历该对象中的所有数据,即“状态”、“0”和“1”。我现在拥有的(和工作的)如下,我知道必须有一种更好的方法来查看元素是否只有一个深度,例如“状态”或者它是否是一个对象,例如“0”和“1” ':

// Prints the link from '0' and '1'
$.each(test, function(){
if(this == '[object Object]')
 alert(this.link);
});

【问题讨论】:

    标签: jquery json


    【解决方案1】:
    for (var propName in object) { var prop = 对象[propName]; if (typeof prop == "object") ... }

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2015-07-04
      • 2014-07-10
      • 2013-05-28
      相关资源
      最近更新 更多