SPLIT data if AJAX post is a JSON encoded array

0 favourites
  • 8 posts
From the Asset Store
Easy SiteLock
$11.99 USD
Protect your game from being stolen and posted on another site with Easy SiteLock!
  • Hi community,

    I have been trying to figure this for an hour with no progress, hopefully an array WIZ can help.

    Problem:

    If an AJAX post is a JSON encoded array, how does one split or use the data/values individually for this array through C2?

    For example; What if i wanted to only display the second value or "ORANGE" only?

    Files:

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

    LINK

    Here is the PHP that C2 loads:

    PHP LINK

    And here is the .CAPX file to manipulate. (These are only two simple events that post the data through AJAX)

    .CAPX

    Thank you in advanced for the help!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I can't open your CAPX because you're on a beta or something...

    Are you trying to bring the array into a C2 app? or push it out?

  • You can use the new regex functionalities C2 offers !

    RegexMatchAt(Text.Text, "(?:"")(.+?)(?:"")", "g", index)

    Where index is the 0-based index of the element you want and Text.text your string to manipulate.

  • goblynn93: Yes, i am trying to bring the array to C2, the JSON encoded array is loaded through an AJAX post, you can see it in the URL:

    LINK

    What i don't know is how to split the data to use it inside of C2, how is this done?

    Magistross: I am not familiar with regex, i will look into it, but like i mentioned, this is uncharted territory for me.

  • If regex confounds you, try a regular split instead with :

    tokenat(Text, Index, Separator)

    You can then remove unwanted characters with replace()

  • Magistross thank you for your help. It is now clear to me that JSON arrays have to be written in a C2 specific format.

    Can you verify if this has been written correctly?:

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

    Second, Once this PHP is loaded, how can i display both the question and the answer as two separate text objects, in other words how do i split or use the data to display the answer and question separately?

    Again, thank you for your continued help...

  • If your JSON string is correctly written (and it seems so), you can load it into an array using the JSON - Load action. Just provide the AJAX response as your string. Once it is loaded, you'll only have to index your array accordingly. (i.e. (0,0) for first question, (0,1) for first answer, etc.)

  • I used CSV2ARRAY plug in to bring in a much easier to write/maintain CSV file... works great.

    Theoretically you could use this to write JSON files for you.

    Create your data array in a spreadsheet.

    Google Docs or Excel or whatever.

    Export to CSV.

    Use CSV2ARRAY to write the CSV file into an array.

    Then use the Array.AsJSON or Array.Download to produce what you need for the JSON file to load.

    I wonder if this makes sense...

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