2013-11-28 44 views

回答

1
$.getJSON("https://www.googleapis.com/freebase/v1/search?query=us%20president&filter=(all%20type:/people/person)&output=(/common/topic/image)", function (data) { 
    var r = data.result; 
    // iterate each result 
    $.each(r, function (i, j) { 
     var arr = j.output['/common/topic/image']; 
     //iterate each arr['/common/topic/image'] 
     $.each(arr['/common/topic/image'], function (k, l) { 
      console.log(l); 
     }); 
    }); 
}); 

JSFiddle

从您的意见,

如果你检查你的json,最后result没有数组叫做/common/topic/image

因此,当它试图迭代,它失败[无法获得长度]

所以只需添加一个条件,如if (arr['/common/topic/image']) {...}。现在,如果它有空元素,它将被跳过。

检查此小提琴http://jsfiddle.net/praveen_jegan/6MCKA/2/

+1

Thanx Praveen Jeganathan。 – ARV

+0

@ARV我很高兴能够帮助我的朋友。 – Praveen

+0

你好Praveen Jeganathan我得到的错误,“Uncaught TypeError:无法读取属性的'未定义的长度'在$ .each(arr ['/ common/topic/image'],函数(索引,图像){} – ARV

0

如果对象键包含点或斜线,您可以访问它以这样的方式

var o = { 
'some.key': 123, 
'another/key': 321 
}; 

console.log(o['some.key'], o['another/key']);