所以我正在使用防暴游戏API来制作类似于lolking.com的东西,这是一个2部分的问题,所以我从它们那里获取json数据,但问题是得到的数据是来自这里的召唤者https://prod.api.pvp.net/api/lol/eune/v1.2/stats/by-summoner/id/summary?api_key=API_KEY_HERE你需要从这里得到他的身份https://prod.api.pvp.net/api/lol/eune/v1.1/summoner/by-name/name?api_key=API_KEY_HERE,所以我猜这是使用$ .getJSON的2部分循环,所以这里是带注释的代码
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
var input = "netuetamundis";
$(document).ready(function(){
// get json from this page to get the ID of the input
$.getJSON("https://prod.api.pvp.net/api/lol/eune/v1.1/summoner/by-name/"+input+"?api_key=b05c2251-f659-4d24-8b5f-6b25a482b42a" , function(name){
// array to put the json data in
var sName = [];
$.each(name, function(key, value){
//put the json data in the array
sName.push(value);
var sID = sName[0];
//get the json from other page, now using the id of the summoner
$.getJSON("https://prod.api.pvp.net/api/lol/eune/v1.2/stats/by-summoner/" + sID + "/summary?api_key=b05c2251-f659-4d24-8b5f-6b25a482b42a" , function(stats){
$.each(stats, function(key, value){
//print out the data
$("div").append("<b>" + JSON.stringify(key) + ": " + JSON.stringify(value) + "</b></br>");
});
});
});
});
});
</script>
</head>
<body>
<div> </div>
</body>
</html>
所以其中一个问题是$("div").append("<b>" + JSON.stringify(key) + ": " + JSON.stringify(value) + "</b></br>");
重复了6次,我认为这与循环有关,第二个问题是我不知道如何通过我试过的第二个json导航做类似值[1]的事情,但它返回了像这样的整个数据块
"summonerId": undefined
"playerStatSummaries": {"playerStatSummaryType":"CoopVsAI","wins":6,"losses":0,"modifyDate":1323084313000,"aggregatedStats":{"totalChampionKills":13,"totalMinionKills":363,"totalTurretsKilled":5,"totalNeutralMinionsKilled":67,"totalAssists":40}}
"summonerId": undefined
"playerStatSummaries": {"playerStatSummaryType":"CoopVsAI","wins":6,"losses":0,"modifyDate":1323084313000,"aggregatedStats":{"totalChampionKills":13,"totalMinionKills":363,"totalTurretsKilled":5,"totalNeutralMinionsKilled":67,"totalAssists":40}}
"summonerId": undefined
"playerStatSummaries": {"playerStatSummaryType":"CoopVsAI","wins":6,"losses":0,"modifyDate":1323084313000,"aggregatedStats":{"totalChampionKills":13,"totalMinionKills":363,"totalTurretsKilled":5,"totalNeutralMinionsKilled":67,"totalAssists":40}}
"summonerId": undefined
"playerStatSummaries": {"playerStatSummaryType":"CoopVsAI","wins":6,"losses":0,"modifyDate":1323084313000,"aggregatedStats":{"totalChampionKills":13,"totalMinionKills":363,"totalTurretsKilled":5,"totalNeutralMinionsKilled":67,"totalAssists":40}}
"summonerId": undefined
"playerStatSummaries": {"playerStatSummaryType":"CoopVsAI","wins":6,"losses":0,"modifyDate":1323084313000,"aggregatedStats":{"totalChampionKills":13,"totalMinionKills":363,"totalTurretsKilled":5,"totalNeutralMinionsKilled":67,"totalAssists":40}}
"summonerId": undefined
"playerStatSummaries": {"playerStatSummaryType":"CoopVsAI","wins":6,"losses":0,"modifyDate":1323084313000,"aggregatedStats":{"totalChampionKills":13,"totalMinionKills":363,"totalTurretsKilled":5,"totalNeutralMinionsKilled":67,"totalAssists":40}}
答案 0 :(得分:0)
试试这个脚本
<script>
var input = "netuetamundis";
$(document).ready(function(){
// get json from this page to get the ID of the input
$.getJSON("https://prod.api.pvp.net/api/lol/eune/v1.1/summoner/by-name/netuetamundis? api_key=b05c2251-f659-4d24-8b5f-6b25a482b42a", function (name) {
// array to put the json data in
var sName = [];
$.each(name, function(key, value){
//put the json data in the array
sName.push(value);
var sID = sName[0];
//get the json from other page, now using the id of the summoner
$.getJSON("https://prod.api.pvp.net/api/lol/eune/v1.2/stats/by-summoner/" + sID + "/summary?api_key=b05c2251-f659-4d24-8b5f-6b25a482b42a" , function(stats){
debugger;
$.each(stats.playerStatSummaries, function (key, value) {
//print out the data
$.each(value, function (key1, value1) {
$("div").append("<b>" + JSON.stringify(key1) + ": " + JSON.stringify(value1) + "</b></br>");
});
});
});
});
});
});
并确保你不像你在这里那样给你api键