How to work with values in a JSON encoded array?

0 favourites
  • 6 posts
From the Asset Store
This Student Workbook uses both Construct 3 & 2 encoding. It supplies client-side & php "back-end" encoding.
  • 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!

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

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

  • 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.html

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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...

  • 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.html

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)