如何在nodejs中解析json中的动态id字段

时间:2014-06-13 17:50:11

标签: json node.js wikipedia-api

我正在尝试使用维基百科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都会改变,我不知道如何访问提取。

1 个答案:

答案 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);
}