How to read JSON data on C2

Get help using Construct 2

Post » Wed Feb 26, 2014 3:58 pm

Greetings, im struggling so hard with this, i read all kind of tutorial, guide and documentation but still can't save JSON data on variables. On the Debug my Dictionary returns "v" and "t" as 0.

I have the following data:

Code: Select all
{"v":999,"t":"3c95aec4c43ea1502c3700f846aa82d0"}


Here is what im doing,
Image

I just need a easy way to do this.
What im doing wrong ? Thanks !
B
4
Posts: 4
Reputation: 283

Post » Wed Feb 26, 2014 4:03 pm

You can load from JSON like you do, but your JSON has to be compatible with the JSON that Dictionary wants. To know the exact format of the dictionary JSON, manually set your keys v ant t to the wanted value with events, then use the "Download" action on the dictionary, that will download on your computer a JSON file, with the format needed.
Once you have this format, change your script on ajaxGame.php to fit what you need.

We did have some explanation of the same process yesterday, but with an array instead of a dictionary. As the process is the same, here is the link for more details.
B
17
S
8
G
4
Posts: 461
Reputation: 6,157

Post » Wed Feb 26, 2014 4:21 pm

Is extremely complicated to understand a long process like that, isnt there a plugin or something to easy read the JSON data without altering my php script ?
B
4
Posts: 4
Reputation: 283

Post » Wed Feb 26, 2014 4:26 pm

JSON Dictionary is some what a pain in the ready. But I can help out with a work around I found. I can't remember the EXACT pre and post fix, but you can find it easier.

create a c2 dictionary

var script = "{""c2dictionary"":true,""data"":"
script var add your json code
script var add "}"

Dictionary.loadJson(script)

I found this work around by mandatory requiremetns. Strangly enough you can embed a natural JSON code into a C2 Dictionary JSON by doing it in the above way. Makes life with JSON so much easier.

However there is one rule to keep in mind. Dictionary can only support 1 level deep. So you can embed data inside an internal object.

good
{
"data"; 0,
"foo": 1
}

bad
{
"object" : {"goo", 2}
"foo" : 1
}
object won't be allowed.
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,018

Post » Wed Feb 26, 2014 4:29 pm

It's not that complicated... Did you try it at least ?
1/ To know the exact format of the dictionary JSON, manually set your keys v ant t to the wanted value with events, then use the "Download" action on the dictionary, that will download on your computer a JSON file, with the format needed.
Capx showing this step
JSON data returned :
Code: Select all
{"c2dictionary":true,"data":{"v":100,"t":"lalalala"}}


2/ Once you have this format, change your script on ajaxGame.php to fit what you need.
So in fact, just add :
Code: Select all
{"c2dictionary":true,"data":

before your current output, and } after it, in your php script.

Took 5 minutes.
B
17
S
8
G
4
Posts: 461
Reputation: 6,157


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests