How do I format my .json file of array data?

Just started using Construct 2? Post your questions here

Post » Sun Sep 21, 2014 12:42 am

I'm searching and searching and not finding enough info to make this work. I have an Ajax object that is loading a .json file of array data. In debug, I see the the ajax.lastdata value does contain the json data, however my format must be wrong because when I try to array.load the data, it doesn't parse right.

Here is what I have in my array json file...
Code: Select all
{
   "c2array": true,
   "size": [2,4,0],
   "data": [
      ["right", 0],
      ["backward", 90],
      ["left", 180],
      ["forward", -90]
   ]
}

I'm trying make an array with 2 values on the X axis and 4 values on the Y axis and the above is obviously wrong. Can you tell me what's wrong with my format?

Thanks very, very much for your help :-)

Mark
B
14
S
4
Posts: 297
Reputation: 1,637

Post » Sun Sep 21, 2014 2:08 am

This is 4 values on the x axis and 1 value on the y axis
{"c2array":true,"size":[4,2,1],"data":[[["left"],[180]],[["right"],[0]],[["forward"],[-90]],[["backwards"],[90]]]}

It looks like your using the x/z axis'.
{"c2array":true,"size":[4,1,2],"data":[[["left",180]],[["right",0]],[["forward",-90]],[["backwards",90]]]}
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sun Sep 21, 2014 2:16 am

So I'm wrong in thinking the size attrib is size: [x,y,z] ? Or my array structure is wrong? I've built a lot of JS arrays so I thought I was building this correctly, but clearly I'm not :-/
B
14
S
4
Posts: 297
Reputation: 1,637

Post » Sun Sep 21, 2014 2:26 am

It's a little odd at first, you get used to it. Just leave size's at 1 otherwise your data won't be loaded..
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sun Sep 21, 2014 2:02 pm

I'll give that a try later when I get home. Thank you for your help!
B
14
S
4
Posts: 297
Reputation: 1,637

Post » Sat Sep 27, 2014 4:40 pm

No variation of the "size" attrib values works. I can see in the array in the debugger that it doesn't have the 2 columns and 4 rows I'm expecting. It has one row with the letter "r". I have no idea what's happening. I suppose these array JSON structures for C2 are not what you would normally build when hand coding in javascript.

Can someone post an example JSON file layout, like above that is actually loading and working in C2? Please :-)
B
14
S
4
Posts: 297
Reputation: 1,637

Post » Sat Sep 27, 2014 5:26 pm

Image
If that doesn't work, maybe you'd be more comfortable with the json plugin?
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sat Sep 27, 2014 7:34 pm

What is wrong with the basic JSON structure in my OP? Are my attribs named correctly? Am I missing any required attrib? Clearly my 2x4 array structure needs some different format that inlcudes the "z" axis. Can someone post the proper JSON for this?

Thanks a lot for your help :-)
B
14
S
4
Posts: 297
Reputation: 1,637

Post » Sat Sep 27, 2014 7:52 pm

Your array loads by swapping the size values. [4,2,1]
Open the debugger and inspect your arrays or you can dump Array.asJSON to text object
{"c2array":true,"size":[4,2,1],"data":[["right",0],["backward",90],["left",180],["forward",-90]]}
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sat Sep 27, 2014 9:40 pm

Ok, following @Noncentz705 advice, I built the array in code, then wrote out the JSON with Array.asJSON. Looking in the debugger, this produces a JSON structure like so...
Code: Select all
{
   "c2array": true,
   "size": [2,4,1],
   "data": [
      [
         ["right"],["backward"],["left"],["forward"]
      ],
      [
         [0],[90],[180],[-90]
      ]
   ]
}

Which is visually bizarre. Comparing the above JSON to what the array actually looks like is very confusing. You visualize arrays as a grid with X-axis running horizontally left to right, Y-axis running vertically top to bottom. The above looks nothing like that.

Here is the JSON from above with some comments to help...
Code: Select all
{
   "c2array": true,
   "size": [2,4,1], // these are array dimensions: x, y, z
   "data": [
      [ // x-axis value, x = 0
         ["right"],["backward"],["left"],["forward"] // y-axis values 0 - 3
      ],
      [ // x-axis value, x = 1
         [0],[90],[180],[-90] // y-axis values 0 - 3
      ]
   ]
}

I'm not 100% sure where the z-axis is above?!

Big thanks to @Noncentz705 for getting me over the hump! :)
B
14
S
4
Posts: 297
Reputation: 1,637


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 3 guests