Array -> Load from JSON.lastData issue

0 favourites
  • 4 posts
From the Asset Store
Various Bell Sounds (from small to huge) / 35 Unique Clips / 2:46 minutes of audio
  • 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 ?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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 ?

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

    {"c2dictionary":true,"data":{"name":"Sonic","speed":5000,"cost":200}}[/code:x07a2iq4]
    
    [code:x07a2iq4]<?php
        function c2DictionaryAsJSON($data) {
          return json_encode(["c2dictionary" => true, "data" => $data]);
        }
    	
        $data = ["name" => "Sonic", "speed" => 5000, "cost" => 200];
    
        echo c2DictionaryAsJSON($data);
    ?>
    [/code:x07a2iq4]
  • How I didn't thought about that !

    Thank you

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)