SPLIT data if AJAX post is a JSON encoded array

Get help using Construct 2

Post » Tue Aug 20, 2013 2:42 am

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!
B
106
S
20
G
8
Posts: 155
Reputation: 11,716

Post » Tue Aug 20, 2013 6:55 am

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?
B
10
S
4
G
2
Posts: 133
Reputation: 2,667

Post » Tue Aug 20, 2013 5:43 pm

You can use the new regex functionalities C2 offers !

[code]RegexMatchAt(Text.Text, "(?:"")(.+?)(?:"")", "g", index)[/code]

Where index is the 0-based index of the element you want and Text.text your string to manipulate.
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Tue Aug 20, 2013 10:20 pm

@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.
Animate2013-08-20 22:22:11
B
106
S
20
G
8
Posts: 155
Reputation: 11,716

Post » Wed Aug 21, 2013 2:39 pm

If regex confounds you, try a regular split instead with : [code]tokenat(Text, Index, Separator)[/code]You can then remove unwanted characters with replace()Magistross2013-08-21 14:39:19
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Wed Aug 28, 2013 9:05 pm

@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...
B
106
S
20
G
8
Posts: 155
Reputation: 11,716

Post » Thu Aug 29, 2013 2:49 pm

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.)Magistross2013-08-29 19:25:59
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Sun Sep 15, 2013 3:43 am

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...
B
10
S
4
G
2
Posts: 133
Reputation: 2,667


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, diamantico21, ratboy3, yolkgames and 32 guests