I made an API request using AJAX request, I received JSON, I tried my best to use JavaScript to parse it, but to no avail. Instead I used jQuery which made it as painless as 2 lines of code.
Now I have JSON array translated into array-ish object in JavaScript. But looking at this script:
obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
It creates a new problem. See, my array is based on name, then on a number. Let me put it another way$gamerData["Aeonix"][0]["kills"]
. Which would be (I have a wild guess right now). obj.aeonix.0.kills
(it looks kinda wrong, correct me on this if you may). But the "Aeonix" part is actually variable, it changes all the time, I can't hardcode the name into the program, it needs to take granted variable and use it within. Something like this obj. + myPlayerName + . + i + . kills
would be a solution.
But it can't be right. Any idea how to retrieve $gamerData["Aeonix"][0]["kills"]
, where [0]
and ["Aeonix"]
can be changed dynamically (different name on each request (variable with name provided), different number on each for()
loop).
The idealized query, something that I get as response (a part of it), is:{"riotschmick":{"id":585897,"name":"RiotSchmick","profileIconId":903,"summonerLevel":30,"revisionDate":1438757819000}}
For example, I would need revisionDate
from received JSON, but I need to go through riotschmick
first. I can't hardcode it, however I have it saved in a variable.
Normally it would be as easy as echo $gamerData[$gamerName][$matchId]["kills"]
in PHP, but I can't get idea how to do it in JavaScript. So far, for JSON -> JavaScript translation, I use already mentioned two liner (will modify as soon as I get answer to this question, which is really important on how to proceed further):
var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert(obj.name === "John" );
It's kind of hard to explain, hopefully you understand.