问题描述
我使用谷歌图书API接收一个书单,但有时也会出现一些图书条目没有某些键/属性,例如,作者或不具有的缩略图。这样的JavaScript说,物业IM试图访问是不确定的和我的应用程序stucks。
示例<一个href=\"https://www.googleapis.com/books/v1/volumes?q=java&callback=jQuery191012062033754773438_1377508316730&_=1377508316731\"相对=nofollow> JSON数据从包含图书搜索例如keywork的Java
全部链接
<$p$p><$c$c>https://www.googleapis.com/books/v1/volumes?q=java&callback=jQuery191020691258599981666_1377508821982&_=1377508821983如,当作者缺少
类型错误:row.volumeInfo.authors未定义
我tryied两个解决方案建议
如果(typeof运算(作者)!=未定义){}
和
如果('作者在booksObject){}
但其中非似乎工作,因为他们即使在这个proerty存在不会进入循环。
这就是我称之为
函数populateListview(书籍){//遍历每个返回项目
$。每个(books.items,功能(我行){ //填充每个行中的列表
VAR htmlString ='&LT;立GT;&LT; A HREF =book_details.html ID ='+ row.id +'&GT;&LT; IMG SRC =; htmlString + = row.volumeInfo.imageLinks.thumbnail +'';
htmlString + ='类=UI丽有拇指/&GT;&LT; H3&GT;';
//检查是否作者在JSON存在
htmlString + = row.volumeInfo.title +'&下; / H3&GT;&所述p为H.;' + row.volumeInfo.authors [0] +'&LT; / P&GT;&LT; / A&GT;&LT; /李&GT;'; //如果不是在作者补充一个未定义的字符串作者
的console.log(htmlString); //新的HTML追加到列表
$('#book_list')追加(htmlString)。
});$('#book_list')的ListView('刷新');
//刷新列表视图等等新元素被添加到DOM
};
您可以检查 $ IsArray的(row.volumeInfo.authors)及&安培; row.volumeInfo.authors.length&GT; 0
的console.log(books.toString());
//遍历每个返回项目
$。每个(books.items,功能(我行){ //填充每个行中的列表
VAR htmlString ='&LT;立GT;&LT; A HREF =book_details.html ID ='+ row.id
+&GT;&下; IMG SRC ='; htmlString + = row.volumeInfo.imageLinks.thumbnail +'';
htmlString + ='类=UI丽有拇指/&GT;&LT; H3&GT;';
如果($ .isArray(row.volumeInfo.authors)及&放大器; row.volumeInfo.authors.length大于0){
// code为了什么在这里使用JSON做
htmlString + = row.volumeInfo.title +'&下; / H3&GT;&所述p为H.;'
+ row.volumeInfo.authors [0] +'&LT; / P&GT;&LT; / A&GT;&LT; /李&GT;';
}其他{
htmlString + = row.volumeInfo.title +'&下; / H3&GT;&所述p为H.;' +作者Undifined
+'&LT; / P&GT;&LT; / A&GT;&LT; /李&GT;';
} 的console.log(htmlString); //新的HTML追加到列表
$('#book_list')追加(htmlString)。
});$('#book_list')的ListView('刷新');
//刷新列表视图等等新元素被添加到DOM
I am using Google Books API to receive a list of books, but sometimes some book entry does not have some keys/properties, e.g., Authors, or does not have a Thumbnail. Thus JavaScript says that the property im trying to access is undefined and my application stucks.
Example Json data example from a book search containing the keywork Java
Full link
https://www.googleapis.com/books/v1/volumes?q=java&callback=jQuery191020691258599981666_1377508821982&_=1377508821983
E.g., when Authors is missing
TypeError: row.volumeInfo.authors is undefined
I tryied two solutions suggested
if ( typeof (authors) != "undefined"){}
and
if('authors' in booksObject) {}
but non of them seems to work, since they never enter the loops even when this proerty exists.
This is where I call
function populateListview(books) {
//iterate each returned item
$.each(books.items, function(i, row) {
//populate each row in the list
var htmlString = '<li><a href="book_details.html?id=' + row.id + '"><img src="';
htmlString += row.volumeInfo.imageLinks.thumbnail + '"';
htmlString += 'class="ui-li-has-thumb"/><h3>';
//Check if authors exists in JSON
htmlString += row.volumeInfo.title + '</h3><p>' + row.volumeInfo.authors[0] + '</p></a></li>';
//If not add an undefined authors string in the authors
console.log(htmlString);
//append new html to the list
$('#book_list').append(htmlString);
});
$('#book_list').listview('refresh');
// refresh the list-view so new elements are added to the DOM
};
You can check $.isArray(row.volumeInfo.authors) && row.volumeInfo.authors.length > 0
console.log(books.toString());
// iterate each returned item
$.each(books.items, function(i, row) {
// populate each row in the list
var htmlString = '<li><a href="book_details.html?id=' + row.id
+ '"><img src="';
htmlString += row.volumeInfo.imageLinks.thumbnail + '"';
htmlString += 'class="ui-li-has-thumb"/><h3>';
if ($.isArray(row.volumeInfo.authors) && row.volumeInfo.authors.length > 0) {
// code for what to do with json here
htmlString += row.volumeInfo.title + '</h3><p>'
+ row.volumeInfo.authors[0] + '</p></a></li>';
} else {
htmlString += row.volumeInfo.title + '</h3><p>' + "Author Undifined"
+ '</p></a></li>';
}
console.log(htmlString);
// append new html to the list
$('#book_list').append(htmlString);
});
$('#book_list').listview('refresh');
// refresh the list-view so new elements are added to the DOM
这篇关于如何检查是否值/属性JSON数据存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!