How do I Load A JSON string into my array?

0 favourites
  • 2 posts
From the Asset Store
A collection of a mix of percussion and orchestral string loops, including 20 musical sketches.
  • 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Make sure your ecapsulating the php array in the format that the C2 array is expecting.

     $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);
    [/code:1gjt5210]
    
    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
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)