繁体   English   中英

使用jQuery在JSON中导航

[英]navigating in json using jQuery

所以我正在使用防暴游戏API对lolking.com进行类似的处理,这是一个两部分的问题,所以我从它们中获取json数据,但事实是要从此处获取召唤师的数据https://prod.api.pvp.net/api/lol/eune/v1.2/stats/by-summoner/id/summary?api_key=API_KEY_HERE您需要从此处获取他的ID https://prod.api .pvp.net / api / lol / eune / v1.1 / summoner / by-name / name?api_key = API_KEY_HERE ,所以我想这是使用$ .getJSON的两部分循环,因此这是带注释的代码

<!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导航,我尝试做类似value [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}}

试试这个脚本

 <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键

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM