我想解析下面的jQuery中的freebase API。解析JSON在JavaScript中的键包含反斜杠
但是我有一个问题,当我尝试访问/common/topic/image
“MID”的形象。
任何机构可以告诉我什么是正确的方式来访问对象或数组,这些类型的键包含"/common/topic/image"
。
谢谢!
我想解析下面的jQuery中的freebase API。解析JSON在JavaScript中的键包含反斜杠
但是我有一个问题,当我尝试访问/common/topic/image
“MID”的形象。
任何机构可以告诉我什么是正确的方式来访问对象或数组,这些类型的键包含"/common/topic/image"
。
谢谢!
$.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);
});
});
});
从您的意见,
如果你检查你的json
,最后result
没有数组叫做/common/topic/image
。
因此,当它试图迭代,它失败[无法获得长度]。
所以只需添加一个条件,如if (arr['/common/topic/image']) {...}
。现在,如果它有空元素,它将被跳过。
如果对象键包含点或斜线,您可以访问它以这样的方式
var o = {
'some.key': 123,
'another/key': 321
};
console.log(o['some.key'], o['another/key']);
Thanx Praveen Jeganathan。 – ARV
@ARV我很高兴能够帮助我的朋友。 – Praveen
你好Praveen Jeganathan我得到的错误,“Uncaught TypeError:无法读取属性的'未定义的长度'在$ .each(arr ['/ common/topic/image'],函数(索引,图像){} – ARV