如果我運行此:我將如何獲得嵌套數組中特定對象的列表?
var url="https://en.wikipedia.org/w/api.php?format=json&action=query&prop=categories&titles=Victory_Tests&callback=?";
$.getJSON(url,function(data){
$.each(data, function(i, item) {
console.log(item);
});
});
控制檯給我:
{pages: {…}}
pages: 2347232:
categories: Array(8)0:
{ns: 14, title: "Category:Aftermath of World War II in the United Kingdom"}
1: {ns: 14, title: "Category:All articles with unsourced statements"}
2: {ns: 14, title: "Category:Articles with unsourced statements from August 2015"}
3: {ns: 14, title: "Category:Articles with unsourced statements from March 2009"}
4: {ns: 14, title: "Category:English cricket seasons from 1919 to 1945"}
5: {ns: 14, title: "Category:History of Test cricket"}
6: {ns: 14, title: "Category:Use British English from September 2011"}
7: {ns: 14, title: "Category:Use dmy dates from September 2011"}
length: 8
__proto__: Array(0)
ns: 0
pageid: 2347232
title: "Victory Tests"
__proto__:
Object__proto__:
Object__proto__: Object
我需要得到所有categories
我試圖item.categories
和
data.parse.pages.map(function(val){
return val.categories[0];
}));
但它不是右邊
所以'的console.log(Object.values(data.query.pages)[0] .categories.map( cat => cat.title.substr(9)));'會給我每一個? –
也json返回罰款,沒有錯誤 –
返回第一頁的類別標題數組。我將刪除回調參數,否則返回一個不必要的'/ ** /(...)',嚴格來說,它不再是JSON,儘管'getJSON'仍然解析它。 – Stephan