How do I Load A JSON string into my array?

Get help using Construct 2

Post » Sat Jul 12, 2014 6:35 pm

I know this should be fairly basic so I must be doing something wrong.

I'm sending a json_encoded string from my php file to C2. The string shows [4,3,1,3,1,2,1,1,0,3] when C2 receives it (when set in a text object). It's just 10 numbers in PHP.

I then try to load it into my C2 array with Array -> Load from JSON string (String).

The array is still empty, so I'm doing something wrong. Any ideas?
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Sat Jul 12, 2014 8:47 pm

Make sure your ecapsulating the php array in the format that the C2 array is expecting.

Code: Select all
 $myArray = array();
    $myArray[] = 4;
    $myArray[] = 3;
    $myArray[] = 1;
    $myArray[] = 3;
    $myArray[] = 1;

    $output = array(
   'c2array' => true,
   'size' => array(
       0 => count($myArray),
       1 => 1,
       2 => 1
   ),
   'data' => array()
    );

    $x = 0;
    foreach ($myArray as $value) {
   $output['data'][$x] = array();
   $output['data'][$x][0] = array();
   $output['data'][$x][0][0] = $value;
   $x++;
    }

    return json_encode($output);


The resulting JSON string would look like this :
{"c2array":true,"size":[5,1,1],"data":[[[4]],[[3]],[[1]],[[3]],[[1]]]}

You can now take this string as is and load to the Array in C2
B
20
S
7
G
1
Posts: 221
Reputation: 2,077


Return to How do I....?

Who is online

Users browsing this forum: John Cutter and 5 guests