Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

javascript - How to check if a value/property exist in JSON data

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
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...