【问题标题】:How to check if a value/property exist in JSON data如何检查 JSON 数据中是否存在值/属性
【发布时间】:2013-08-28 17:31:10
【问题描述】:

我正在使用 Google Books API 接收图书列表,但有时某些图书条目没有某些键/属性,例如 Authors,或者没有缩略图。因此 JavaScript 说我试图访问的属性是未定义的,我的应用程序卡住了。

例如Json data example from a book search containing the keywork Java

完整链接

https://www.googleapis.com/books/v1/volumes?q=java&callback=jQuery191020691258599981666_1377508821982&_=1377508821983

例如,当 作者 丢失时

TypeError: row.volumeInfo.authors is undefined

我尝试了两个建议的解决方案

if ( typeof (authors) != "undefined"){}

和

if('authors' in booksObject) {}

但它们似乎都不起作用,因为即使存在此属性,它们也不会进入循环。

这是我打电话的地方

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
};

【问题讨论】:

    标签: javascript jquery json api dom


    【解决方案1】:

    您可以查看$.isArray(row.volumeInfo.authors) &amp;&amp; row.volumeInfo.authors.length &gt; 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 转换为数组。在这种情况下,JSON 只是一个字符串还是一个对象?
    • @LaliPali 它不会转换任何数据...唯一的变化是在if 条件中
    • 是否可以对多行​​执行此操作,即作者、标题、副标题、描述等。如果有的话是“未定义”
    【解决方案2】:

    试试这个:

    if(booksObject.image) {
     //do stuff with it
     } else if( booksObject.author ) {
     //do stuff with author
     }
    

    【讨论】:

      【解决方案3】:

      这是检查 JSON 中是否存在任何键或值的一种非常简单的方法。

      var jsonString = JSON.stringify(yourJSON);
      
      if (jsonString .indexOf("yourKeyOrValue") > -1){
          console.log("your key or value exists!");
      }
      

      它快速、简单、容易。

      【讨论】:

        猜你喜欢
        • 2021-04-22
        • 1970-01-01
        • 1970-01-01
        • 2016-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多