How to work with values in a JSON encoded array?

Get help using Construct 2

Post » Tue Aug 20, 2013 11:52 pm

Hi community,

I am posting through a new thread as my first was probably confusing.
@Ashley , this is what i have not been able to figure out.

PROBLEM TO SOLVE:

Inside of C2, how does one work with individual values of a JSON encoded array which was made from an AJAX response?

FILES:

Here is the URL to view how C2 displays the JSON encoded array:
LINK

Here is the PHP that C2 loads:
PHP

And here is the .CAPX file to manipulate. (Open with the latest beta release.)
.CAPX

So, for example; What if i wanted to split or only display the second value "ORANGE"? How would i do this?

Thank you in advanced for the help!Animate2013-08-20 23:54:04
B
106
S
20
G
8
Posts: 155
Reputation: 11,716

Post » Wed Aug 21, 2013 12:37 pm

It would be easier to send the data in a format that C2 has tools to parse, such as C2's Array JSON format (which isn't just a simple JSON array), the Dictionary object's JSON format, XML, or just a comma separated string.
Scirra Founder
B
397
S
236
G
88
Posts: 24,408
Reputation: 194,506

Post » Sat Aug 24, 2013 5:03 am

@Ashley
How about an official JSON parser plugin with javascript notation? You could have it work like this: getJSONValue("player.attributes.health") or getJSONString("scoreboard[0].player.name")

Should be really simple to do, and sounds like a thing that should be made as an official plugin.Fimbul2013-08-24 05:03:33
B
36
S
8
G
8
Posts: 532
Reputation: 6,903

Post » Sat Aug 24, 2013 4:22 pm

First of all your JSON is unreadable for C2, I added a file with a correct JSON(note that size values indicate how many objects are in the array dimensions, also pay attention to the correct use of [] brackets). I made a request for the .json file in the imported files folder and then displayed all the values with a for each loop just to display them. Now when you press Tab you get the second value of the array. The php request for the array is a much more difficult matter though, I made a project about loading a C2 array with php from mysql table sometime ago and wrote a turorial about it you might want to check it out, the part that migt interest you is in the end.
https://www.scirra.com/tutorials/585/how-to-make-an-ingame-costume-made-level-editor

here is your .capx :
http://www.upload.ee/files/3534732/JSONarray__1_.capx.htmltrollface2013-08-24 16:37:03
B
14
S
6
G
4
Posts: 107
Reputation: 4,838

Post » Wed Aug 28, 2013 8:44 pm

@trollface Thank you, for clarifying that JSON encoded arrays are not readable in C2 and for your changes to my .CAPX

I do have two final questions now knowing that the array has to be written in a specific C2 format:

1- Can you verify that this following array has been written correctly?:

{ "c2array":true, "size":[ 2, 2, 1 ], "data":[ [ ["What is your name?"], ["John"] ], [ ["What color is your hair?"], ["Brown"] ] ] }


2- Inside the .CAPX, how can i display both the question and the answer as two separate text objects, in other words how do i split the data to display answers and questions separately?


Again, thank you for your assistance, it has helped a lot...
B
106
S
20
G
8
Posts: 155
Reputation: 11,716

Post » Thu Aug 29, 2013 5:43 pm

Yes, your array format is correct. To display questions and answers separetely, you have to call them out using both x amd y axis coordinates. I made changes to te capx, so that question is displayed first and then after pressing tab the invisible answeres are made visible.
http://www.upload.ee/files/3547432/JSONarray__1_.capx.htmltrollface2013-08-29 17:43:36
B
14
S
6
G
4
Posts: 107
Reputation: 4,838


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 26 guests