0 Favourites

How to read JSON data on C2

  • 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:

    {"v":999,"t":"3c95aec4c43ea1502c3700f846aa82d0"}[/code:92crdsg1]
    
    Here is what im doing,
    [img="http://i.imgur.com/GfI5RfU.jpg"]
    
    I just need a easy way to do this.
    What im doing wrong ? Thanks !
  • Construct 3

    Buy Construct 3

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

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

  • 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 ?

  • 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.

  • 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 :

    {"c2dictionary":true,"data":{"v":100,"t":"lalalala"}}[/code:1hh3fnv1]
    
    2/ Once you have this format, change your script on ajaxGame.php to fit what you need.
    So in fact, just add :
    [code:1hh3fnv1]{"c2dictionary":true,"data":[/code:1hh3fnv1]
    before your current output, and } after it, in your php script.
    
    Took 5 minutes.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)