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

Get help using Construct 2

Post » Wed Apr 29, 2015 8:42 pm

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.
Image
B
45
S
17
G
2
Posts: 201
Reputation: 4,471

Post » Wed Apr 29, 2015 8:52 pm

So what does your JSON look like?
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,630

Post » Thu Apr 30, 2015 1:27 am

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.
B
9
S
2
Posts: 54
Reputation: 638

Post » Fri May 01, 2015 2:13 pm

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.
Image
B
45
S
17
G
2
Posts: 201
Reputation: 4,471

Post » Sat May 02, 2015 6:03 pm

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
B
9
S
2
Posts: 54
Reputation: 638

Post » Sat May 02, 2015 7:00 pm

May I ask how you converted it into the C2 JSON format?
B
12
S
3
Posts: 211
Reputation: 1,477

Post » Fri Jul 15, 2016 8:28 pm

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

Code: Select all
            {
                   c2dictionary : true,
                    data : {
                       // your data
                    }
             }


The c2dictionary property and data object are required!
B
3
Posts: 1
Reputation: 177


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go and 9 guests