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.

Recommended Answers

All 4 Replies

Member Avatar for diafol

If you get json from api just print out what u get with JSON.stringify to console to see output

It's a bit more complicated than that.

You're on index.php, there's ajax.js attached, if you press buttons, ajax.js will send request to /sys/datarequest.php. And datarequest.php is the actual file that manages the API. Now I need to send that data back to ajax.js, which works, but now it needs to be parsed and put into array, so I can then easily move these "contents" into correct places.

I have it all hidden on the back of my hand. But parsing incoming JSON from my own file.

just print out what u get with JSON.stringify to console to see output

Uhm, but I don't want it in console, I need it in variable (an array), or am I not getting something?

But sure, brb.

> JSON.stringify
< function stringify()

I did it wrong, I should paste JSON.stringify into console, right?

JSON.stringify according to many websites, turns flat text into JSON query (what?). But I already have JSON response, I just need to find equivalent to json_decode(); in JavaScript, which apparently isn't that easy :|

I know where I went wrong, when displaying JSON response I used var_dump() which also produces meta-data, the length of each dimension and data type. I used print_r() instead to force turning object into a string, and then just single line: var data = JSON.parse(data) then sorted in the array where I can request it and use innerHTML=; to put it where I want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.