如果我运行此:我将如何获得嵌套数组中特定对象的列表?
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