How do I use AJAX to load a JSON into a Dictionary object?

0 favourites
  • 7 posts
From the Asset Store
112 High-Quality destruction sounds for videogames
  • So I have a database set up that stores user information. I'm using AJAX to request that information which comes back to me in the form of a JSON. I need to take that JSON and populate a dictionary object with it.

    My Code

    On Start of Layout -> AJAX: Request "http://mywebsite/JSONInfo.php" (tag "load")

    AJAX On "load" completed -> Dictionary Load from JSON string AJAX.LastData

    I can take the JSON that is being returned copy and paste the text into the Dictionary Load from JSON string and it works fine. But when I use AJAX.LastData it fails. I tried storing the AJAX Data into a variable and telling the Dictionary object to load the variable and still nothing. I also tried putting the JSON into a text object which worked then tried telling the dictionary object to use that text and still it doesn't work.

    So I don't understand why I can copy/paste the data into the dictionary and it works fine. But I cannot put the data into the dictionary using any other means.

  • So what does your JSON look like?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm not sure if this changes anything, but your requesting a php file in your example. typically that will also load the php code.. Shouldn't you be directing it to JSONinfo.JSON .

    Usually you can check by going to your file and downloading it and opening it in a JSON editor......

    Beside that you should also make sure the formatting is the same as Constructs AJAX JSON read.. the easiest way to do this is build the JSON in the game and then debug it and copy that codes format..

    hope this is helpful.

  • It was a formatting issue, Construct doesn't accept a proper JSON but it' own modified JSON. It took a bunch of testing but I was finally able to get the PHP to spit out the JSON in the format construct likes. It was just difficult because the copy/pasted text format of a JSON is different from the raw JSON it expects.

  • Yeah I just had that same problem trying to get my game on Kongregate, the double "" is quite a bit to convert in and out of

  • May I ask how you converted it into the C2 JSON format?

  • I had the same problem. You need the following structure to load json into the dictionary:

                       c2dictionary : true,
                        data : {
                           // your data
    The c2dictionary  property and data object are required!
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)