2

I have an issue when trying to get some data from a json.

Usually I have to hardcode the JSON variable I want (ID), such as

success: function (json) {
    alert(json.ID.name);
}

Which works just fine, however I need to make this dynamic as my ID needs to update per request.

When I try the following

success: function (json) {
    // ID is given above and it is working fine
    var userID = ID.replace(" ", "");
    userID = userID.toLowerCase().trim();
    alert(json.userID.name);
    }

Doing that gives me an error

Uncaught TypeError: Cannot read property 'name' of undefined

The userID when alerted gives the same String as ID, but sticking it in the json does not seem to work. Any reason why?

Thanks!

Example (unfortunately I cannot post my API key)

var ID = "";
ID = $("#theID").val();
if (ID != "") {
$.ajax({
    url: 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' + ID + '?api_key=<KEY>',
    type: 'GET',
    dataType: 'json',
    data: {
        //email: $('#sube').val()
    },
    success: function (json) {
        var userID = ID.replace(" ", "");
        userID = userID.toLowerCase().trim();


        document.getElementById("who").innerHTML = "Who: " + json.userID.name;
        document.getElementById("level").innerHTML = "Level:" + json.userID.id;
        document.getElementById("id").innerHTML = "ID: " + json.userID.summonerLevel;
        document.getElementById("idCall").innerHTML = "Status: SUCCESS";
        document.getElementById("url").innerHTML = "URL: " + 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' + userID + '?api_key=<KEY>';
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
        document.getElementById("idCall").innerHTML = "Status: FAILED";
    }
});
} else {
    document.getElementById("idCall").innerHTML = "Status: FAILED";
}
Austin
  • 3,010
  • 23
  • 62
  • 97
  • please provide sample `userid` and `json`...i think you want ` alert(json[userID].name);` – abc123 Jul 07 '14 at 19:43
  • The way you are doing thats not how things work. Why do you want to change the property `ID` to `userID`? – Rahil Wazir Jul 07 '14 at 19:43
  • 1
    Shouldn't it be `json[userID].name`? – blex Jul 07 '14 at 19:43
  • I think you need to make an array and access it : `json.users[userID]` – gskema Jul 07 '14 at 19:43
  • Note: JSON is just the text format used for the response. It does not have variables and, by using `dataType: 'json'`, will have already been parsed to JavaScript objects and values by the time `function (json) { ... }` is called. – Jonathan Lonowski Jul 07 '14 at 19:49

2 Answers2

4

userID is not a key name in your JSON object. You need to do it like this:

alert(json[userID].name);

Also, remember if your JSON is coming as a string, you need to parse it before you can access it.

joshboley
  • 1,143
  • 6
  • 10
1

What about json[userID].name otherwise you'll get the property userID from your json object and not the value of userID

TobiSH
  • 2,833
  • 3
  • 23
  • 33