Array -> Load from JSON.lastData issue

Get help using Construct 2

Post » Mon May 18, 2015 10:17 am

Hi.
I'm trying to fill an array with some data from a JSON string.

I tread this tutorial and I'm trying to do somewhat the same thing : https://www.scirra.com/tutorials/978/ex ... -game-card

Is is the best way to return data from a JSON string ?
Is there any nicer way to parse the JSON string which would allow us to use string keys instead of numerical indexes that doesn't have any descriptive information ?
Based on what I saw on the tutorial, here is the JSON string I'm loading into an array :

{"c2array": true,
"size": [4,1,1],
"data": ["5","Conno d'Argos","0","Vela ledit cresdit."]
}

I'd rather have for example a js object like this :
{
"points" : "5",
"name" : "Conno d'Argos",
"cost" : 0,
"description" : "Vela ledit cresdit."
}
That I could parse and populate my Construct 2 project objects using these descriptive keys. Is it possible ?

And another question :

Here is the array filled with the Load from JSON event from my JSON string (the first one) :
Array data :
0 : 5
1 : C
2 : 0
3 : V

It takes only the first character of each array element, do you know why ?
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Mon May 18, 2015 11:56 am

Okay I fixed that.
My PHP array should have looked like this :

$card = [
"c2array" => true,
"size" => [4,1,1],
"data" => [[["5"]],[["Conno d'Argos"]],[["0"]],[["Vela ledit cresdit."]]]
];

Instead of :

$card = [
"c2array" => true,
"size" => [4,1,1],
"data" => ["5","Conno d'Argos","0","Vela ledit cresdit."]
];

Still, it's an ugly way to pass variables from PHP to Construct 2. Is there a better way ?
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Mon May 18, 2015 1:48 pm

You could use a dictionary instead of an array for descriptive keys.

Code: Select all
{"c2dictionary":true,"data":{"name":"Sonic","speed":5000,"cost":200}}


Code: Select all
<?php
    function c2DictionaryAsJSON($data) {
      return json_encode(["c2dictionary" => true, "data" => $data]);
    }
   
    $data = ["name" => "Sonic", "speed" => 5000, "cost" => 200];

    echo c2DictionaryAsJSON($data);
?>
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,630

Post » Mon May 18, 2015 2:09 pm

How I didn't thought about that !

Thank you :)
B
12
S
7
G
7
Posts: 450
Reputation: 4,276


Return to How do I....?

Who is online

Users browsing this forum: R0J0hound, Yahoo [Bot] and 9 guests