我正在尝试使用维基百科api - http://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=extracts&explaintext&exintro=&format=json
提取一些信息我得到的回复如下,
{"query":
{"pages":
{"1350786":
{"pageid":1350786,"ns":0,"title":"2005 in Wales","extract":"This article is about the particular significance of the year 2005 to Wales and its people."}}}}
如何遍历"提取" node.js中的字段?我正在尝试使用query.pages。但是由于每次调用我的ID都会改变,我不知道如何访问提取。
答案 0 :(得分:1)
如果您不知道id(页面下的字典键),您只需遍历页面下的所有键即可获取提取字段:
var obj1 =
{
"query":
{
"pages":
{"1350786":
{"pageid":1350786,"ns":0,"title":"2005 in Wales",
"extract":"This article is about the particular significance of the year 2005 to Wales and its people."}
}
}
}
for (var key in obj1.query.pages){
console.log(obj1.query.pages[key].extract);
}