JSON未定义或为null

时间:2016-02-25 13:57:51

标签: javascript arrays json ajax

我试图访问API并继承我要访问的树:

{"19777621": [{
   "queue": "RANKED_SOLO_5x5",
   "name": "Vladimir's Maulers",
   "entries": [{
      "leaguePoints": 0,
      "isFreshBlood": false,
      "isHotStreak": true,
      "division": "I",
      "isInactive": false,
      "isVeteran": false,
      "losses": 34,
      "playerOrTeamName": "Razdiel",
      "playerOrTeamId": "19777621",
      "wins": 36
   }],
   "tier": "PLATINUM"
}]}

我设法做了很多例子,但这是我真的无法弄清楚它是如何工作的,我确定我可以反应体,但如果我尝试做某事它来自未定义的空白或对象。

<head>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script src="/js/json2.js"></script>
    <script src="/js/json_parse.js"></script>
</head>
<body>
<script>
$.ajax({
url: 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/19777621/entry?api_key=b05c2777-462b-4bcc-ac2a-a3223bb74876',
type: 'GET',
dataType: 'json',
data: {

},
success: function (json) {
document.write("The Result Is:")

    JSON_Encoded = json;
    JSON_Decoded = JSON.stringify(json);
    document.write(JSON_Decoded[19777621].name[0])
    document.write(JSON_Decoded[19777621].entries.losses[0])

},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner data!");
}
});

</script>

我知道我做错了什么我只是想知道什么

1 个答案:

答案 0 :(得分:1)

你可以用这种方式写出来......

$.ajax({
url: 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/19777621/entry?api_key=b05c2777-462b-4bcc-ac2a-a3223bb74876',
type: 'GET',
dataType: 'json',
data: {

},
success: function (json){
  document.write("The Result Is:")

  //JSON_Encoded = json;
  //JSON_Decoded = JSON.stringify(json);   
  document.write(json['19777621'][0].name)
  document.write(json['19777621'][0].entries[0].losses)

},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner data!");
}
});