从JSON中获取某个属性的值字符串

get string of values of certain property from JSON

本文关键字:属性 字符串 JSON 获取      更新时间:2023-09-26

我正试图通过Google Books的API从书架上获取一串ISBN。这是我的尝试,但没有成功。(我正在尝试使用这个片段。)

$.getJSON("https://www.googleapis.com/books/v1/users/115939388709512616120/bookshelves/1004/volumes?key=MYAPIKEY", function (data) {
console.log(data);
var allIsbns = [];
for (i = 0; i < data.items.volumeInfo.industryIdentifiers[0].identifier.length; i++) {
allIsbns.push(data.items.volumeInfo.industryIdentifiers[0].identifier[i]);
}
alert(allIsbns);
});

小提琴

查看记录的对象,data.items是一个数组(看起来长度为data.totalItems)。此外,industryIdentifiers[0].identifier似乎是一个字符串,不是一个数组。因此,我认为您希望循环使用data.items

此外,可能值得注意的是,除非规范调用了预定义的顺序,否则您可能不应该在industryIdentifiers上使用显式索引。我建议使用type === "ISBN_10":查找标识符

for (var i = 0; i < data.items.length; i++) {
    for (var j = 0; j < data.items[i].volumeInfo.industryIdentifiers.length; j++) {
        if (data.items[i].volumeInfo.industryIdentifiers[j].type === "ISBN_10")        
            allIsbns.push(data.items[i].volumeInfo.industryIdentifiers[j].identifier);
    }
}