Javascript 如何检查JSON数据中是否存在值/属性
我正在使用Google Books API接收图书列表,但有时有些图书条目没有一些键/属性,例如,作者,或者没有缩略图。因此JavaScript表示我试图访问的属性未定义,我的应用程序阻塞 范例 全链接Javascript 如何检查JSON数据中是否存在值/属性,javascript,jquery,json,api,dom,Javascript,Jquery,Json,Api,Dom,我正在使用Google Books API接收图书列表,但有时有些图书条目没有一些键/属性,例如,作者,或者没有缩略图。因此JavaScript表示我试图访问的属性未定义,我的应用程序阻塞 范例 全链接 https://www.googleapis.com/books/v1/volumes?q=java&callback=jQuery191020691258599981666_1377508821982&_=1377508821983 例如,作者缺失时 TypeError: r
https://www.googleapis.com/books/v1/volumes?q=java&callback=jQuery191020691258599981666_1377508821982&_=1377508821983
例如,作者缺失时
TypeError: row.volumeInfo.authors is undefined
我尝试了两种解决方案
if ( typeof (authors) != "undefined"){}
及
但它们中的任何一个似乎都不起作用,因为即使存在这种能力,它们也不会进入循环
这是我打电话的地方
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
};
函数populateListview(书籍){
//迭代每个返回的项
$.each(books.items,function(i,row){
//填充列表中的每一行
var htmlString='';
//如果没有,请在authors中添加未定义的authors字符串
console.log(htmlString);
//将新html追加到列表中
$(“#图书列表”).append(htmlString);
});
$(“#图书列表”).listview(“刷新”);
//刷新列表视图,以便将新元素添加到DOM中
};
您可以检查$.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
console.log(books.toString());
//迭代每个返回的项
$.each(books.items,function(i,row){
//填充列表中的每一行
var htmlString='';
}否则{
htmlString+=row.volumeInfo.title+''+“作者未定义”
+“”;
}
console.log(htmlString);
//将新html追加到列表中
$(“#图书列表”).append(htmlString);
});
$(“#图书列表”).listview(“刷新”);
//刷新列表视图,以便将新元素添加到DOM中
试试这个:
if(booksObject.image) {
//do stuff with it
} else if( booksObject.author ) {
//do stuff with author
}
这里有一个非常简单的方法来检查JSON中是否存在任何键或值
var jsonString = JSON.stringify(yourJSON);
if (jsonString .indexOf("yourKeyOrValue") > -1){
console.log("your key or value exists!");
}
它快速、简单、容易。此代码是否将JSON转换为数组。在这种情况下,JSON只是一个字符串还是一个对象?@LaliPali它不会转换任何数据。。。唯一的更改是在
条件中,如果条件允许使用多行,即作者、标题、副标题、描述等。。如果有“未定义”
var jsonString = JSON.stringify(yourJSON);
if (jsonString .indexOf("yourKeyOrValue") > -1){
console.log("your key or value exists!");
}