make json array with php [SOLVED]

Get help using Construct 2

Post » Mon Feb 18, 2013 1:45 pm

Hi i have been looking through the forums and can't find anything about making a json array with php that prints anything like the json i have seen here?

This is my php that prints valid json.

$json_data = array();
while ($row = mysql_fetch_assoc($sql)) {
   array_push($json_data, $row);
}
$json_data = json_encode($json_data);
echo $json_data;

The output look like this.

[{"id1":"data1","id2":"data2","id3":"data3","id4":"data4","id5":"data5",""id6":"data6","id7":"data7","id8":"data8","id9":"data9","id10":"data10","id11":"data11","id12":"data12","id13":"data13"}]

After this i make an ajax request for the page.

This dose not work all the json arrays i have seen have ([]) not ("") how can i get php to format it like it should be for construct.

ace53422013-02-18 21:28:03
B
3
Posts: 4
Reputation: 283

Post » Mon Feb 18, 2013 2:30 pm

Beyond the scope of the forums here, really.

I'd suggest if you're using php 5.2 or above to use Json Encode and if you aren't, then do a google search for PHP Json encode function. There are plenty out there you can cut and paste that will properly convert a php array into json.
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Mon Feb 18, 2013 2:51 pm

Yes i am using php 5.2 and json_encode the data I get out is json but it's not formated in the way construct 2 uses. It look like the json_encode encodes the data one way and construct wants it to be formatted in a different way. All the posts I have look at about json_encode the data come out looking the same as I have posted above?
B
3
Posts: 4
Reputation: 283

Post » Mon Feb 18, 2013 3:36 pm

You can't just load a json_encoded array into a C2 array. It has to be in a specific format for C2.

{"c2array":true,"size":[5,1,1],"data":[[[0]],[[0]],[[0]],[[0]],[[0]]]}
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Mon Feb 18, 2013 3:41 pm

Or you use the Hash plugin it got JSON functions
http://www.scirra.com/forum/plugin-hash-table_topic47637_page1.html
localboss2013-02-18 15:42:38
B
15
S
6
G
6
Posts: 512
Reputation: 5,550

Post » Mon Feb 18, 2013 3:58 pm

Thanks ramones I know about that forgot to include it in my post but the problem was the format of the array after theubie's post i looked at php.net about json_encode the php needs to be formated in a certain way for json to encode it as I wanted.

Thanks for both responses.

The soloution is when building the array you have to put it into an array like so

$json_data = array();
while ($row = mysql_fetch_assoc($sql)) {
   $json_data = array(array(array(FIRST BIT OF DATA)), array(array(SECOND BIT OF DATA)), array(array(NEXT BIT OF DATA)), array(array(AND SO ON)));
}
$json_data = json_encode($json_data);
echo '{"c2array":true,"size":[4,1,1],"data":';
echo $json_data;
echo '}';

this example will ouput for a 4 x 1 x 1 array that with a json load in construct 2 will work and load your data in for you.


Sorry localboss did not see your comment there till i come to edit this must have been typing my original out as you commented thanks for the suggestion i have managed to solve this now ace53422013-02-18 21:31:29
B
3
Posts: 4
Reputation: 283

Post » Tue Jan 28, 2014 11:19 am

Thanks a ton !! ace5342...I had similar problem. your solution worked out well..
B
2
Posts: 7
Reputation: 164

Post » Thu Oct 16, 2014 1:29 am

ace5342 wrote:Thanks ramones I know about that forgot to include it in my post but the problem was the format of the array after theubie's post i looked at php.net about json_encode the php needs to be formated in a certain way for json to encode it as I wanted.

Thanks for both responses.

The soloution is when building the array you have to put it into an array like so

$json_data = array();
while ($row = mysql_fetch_assoc($sql)) {
   $json_data = array(array(array(FIRST BIT OF DATA)), array(array(SECOND BIT OF DATA)), array(array(NEXT BIT OF DATA)), array(array(AND SO ON)));
}
$json_data = json_encode($json_data);
echo '{"c2array":true,"size":[4,1,1],"data":';
echo $json_data;
echo '}';

this example will ouput for a 4 x 1 x 1 array that with a json load in construct 2 will work and load your data in for you.


Sorry localboss did not see your comment there till i come to edit this must have been typing my original out as you commented thanks for the suggestion i have managed to solve this now ace53422013-02-18 21:31:29


I have scoured this forum for this solution for days now - thanks so much for sharing!
B
9
S
1
Posts: 33
Reputation: 612


Return to How do I....?

Who is online

Users browsing this forum: Je Fawk, MarkThomas, theosciandra and 20 guests