如果总是返回 different/random 名称,如何绑定到 json 键?
How to bind to a json key if it always gets returned with a different/random name?
我正忙于一个 angular 4.x 应用程序,我试图在其中显示随机的维基百科文章。我在 query/pages 下的 chrome 开发工具中看到的 JSON alwasys 具有不同的编号 pageID。而不是 return 将页面作为一个数组,它们 return 它作为一个 JSON 对象,它总是有一个不同的名称,所以我不确定如何将它变成一个打字稿接口或绑定看到它的名字是不同的吗?知道我该怎么做吗?
我从维基百科上得到的 JSON api:
{
"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>..."
}
}
}
}
使用Object.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)
您可以使用 javascript Object.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)
使用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>"
}
]
}
}
我正忙于一个 angular 4.x 应用程序,我试图在其中显示随机的维基百科文章。我在 query/pages 下的 chrome 开发工具中看到的 JSON alwasys 具有不同的编号 pageID。而不是 return 将页面作为一个数组,它们 return 它作为一个 JSON 对象,它总是有一个不同的名称,所以我不确定如何将它变成一个打字稿接口或绑定看到它的名字是不同的吗?知道我该怎么做吗?
我从维基百科上得到的 JSON api:
{
"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>..."
}
}
}
}
使用Object.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)
您可以使用 javascript Object.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)
使用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>"
}
]
}
}