我正忙着使用一个有角度的4.x应用程序,我试图显示一篇随机的维基百科文章。我在查询/页面alwasys下的chrome dev工具中看到的JSON具有不同的nubered pageID。它不是将页面作为数组返回,而是将它作为JSON对象返回,它总是有一个不同的名称,所以我不知道如何将其转换为打字稿界面或绑定到它,因为名称不同?知道我怎么能这样做吗?
我从维基百科api获得的JSON:
{
"batchcomplete": "",
"continue": {
"grncontinue": "0.241230031087|0.241230543855|19422120|0",
"continue": "grncontinue||"
},
"warnings": {
"extracts": {
"*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
}
},
"query": {
"pages": {
"742585": {
"pageid": 742585,
"ns": 0,
"title": "Speedway",
"extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
}
}
}
}
答案 0 :(得分:4)
使用Object.keys获取Object as Array中的Keys
var data = {
"batchcomplete": "",
"continue": {
"grncontinue": "0.241230031087|0.241230543855|19422120|0",
"continue": "grncontinue||"
},
"warnings": {
"extracts": {
"*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
}
},
"query": {
"pages": {
"742585": {
"pageid": 742585,
"ns": 0,
"title": "Speedway",
"extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
}
}
}
};
var MyArray = Object.keys(data.query.pages);
console.log('MyArray: '+ JSON.stringify(MyArray));
// Required Data
console.log('Page: '+ data.query.pages[MyArray[0]].pageid)
答案 1 :(得分:1)
您可以使用类似
的javascriptObject.keys
方法
var obj={
"batchcomplete": "",
"continue": {
"grncontinue": "0.241230031087|0.241230543855|19422120|0",
"continue": "grncontinue||"
},
"warnings": {
"extracts": {
"*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
}
},
"query": {
"pages": {
"742585": {
"pageid": 742585,
"ns": 0,
"title": "Speedway",
"extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
}
}
}
}
var pageNo=Object.keys(obj.query.pages)[0];
console.log(pageNo)
&#13;
答案 2 :(得分:0)
使用formatversion=2
:
action=query&format=json&prop=extracts&exlimit=1&generator=random&formatversion=2
{
"batchcomplete": true,
"continue": {
"grncontinue": "0.451777061970|0.451777269201|12035865|0",
"continue": "grncontinue||"
},
"query": {
"pages": [
{
"pageid": 18652441,
"ns": 0,
"title": "Kuczyny",
"extract": "<p><b>Kuczyny</b> <span>[kuˈt͡ʂɨnɨ]</span> is a village in the administrative district of Gmina Stawiski, within Kolno County, Podlaskie Voivodeship, in north-eastern Poland. It lies approximately 4 kilometres (2 mi) north of Stawiski, 15 km (9 mi) east of Kolno, and 75 km (47 mi) north-west of the regional capital Białystok.</p>\n<p>The village has a population of 31.</p>\n<h2><span id=\"References\">References</span></h2>\n\n<p><br></p>\n\n<p><span></span></p>"
}
]
}
}