0

Im trying to access a API and heres the tree i want to access:

{"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"
}]}

I managed to do a lot of examples but this is the one i really cant figure out how it works, im sure i can the response body but if i try to do something it comes as undefined blank or Object.

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

I know im doing something wrong i just wanted to know what

8
  • Have you tried using a tool like Postman? It allows you to send POST and GET requests to URL's, and shows you the response. It's really useful for testing API's, most likely your API is giving you an error that isn't in a JSON format. EDIT: Or just use Parse instead of stringify, thanks other comments can't believe I missed that. Commented Feb 25, 2016 at 14:02
  • 1
    JSON.stringify turns an object into a, well, JSON string. The reverse is JSON.parse! And you probably don't need either, since the response will probably already be decoded. Commented Feb 25, 2016 at 14:02
  • when you're accessing your object too you'd want JSON_Decoded[19777621][0].name Commented Feb 25, 2016 at 14:06
  • i did all the changes you guys said and it still doesnt give me anything at all: JSON_Decoded = JSON.parse(json); document.write(JSON_Decoded[19777621][0].name) Commented Feb 25, 2016 at 14:07
  • 1
    thanks deceze i should stop a bit and learn how to debug my own stuff Commented Feb 25, 2016 at 14:32

1 Answer 1

1

You could write it in this way ...

$.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!");
}
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.