[英]How to parse dynamic id fields in json in nodejs
我正在嘗試使用Wikipedia 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都會更改,因此我不確定如何訪問摘錄。
如果您不知道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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.