Page 1 of 1
Wondering how to format JSON in PHP
Posted: Tue Jun 29, 2010 9:09 pm
by Jackolantern
I am banging my head into the wall. I am using jQuery AJAX (although I am not overly concerned with the client-side at the moment, so whether you have experience with jQuery or vanilla JS is not an issue), and have had little if any problems until now. Now I am wanting to return several values from an AJAX call, so I am needing to format them in JSON format.
But my problem is that it keeps failing, so I am looking at the portion I know the least about: formatting a JSON in PHP. I know about the json_encode() function, and I am building an associative array, processing it through json_encode() and echoing it, but it just isn't working. Here is what I have written:
Code: Select all
$sendS = array ("monsterDmg"=>$monsterDmg, "playerDmg"=>$playerDmg, "monsterHP"=>$mobNewHp, "playerHP"=>$playerNewHp );
echo json_encode($sendS);
I tried putting both single and double-quotes around the keys in the assoc array (since JSON demands double-quotes, but I figured the encoding would fix that anyway). Anyone know what is wrong here?
EDIT: The rest of the code is in the file. I am just not including it because it is all passing tests and appears to be working fine.
Re: Wondering how to format JSON in PHP
Posted: Tue Jun 29, 2010 10:05 pm
by Chris
Should you not be using single quotations for your keys?
And show us what it is echoing as there are several different formats you can use which is most likely your problem.
Re: Wondering how to format JSON in PHP
Posted: Tue Jun 29, 2010 10:08 pm
by Jackolantern
Chris wrote:Should you not be using single quotations for your keys?
And show us what it is echoing as there are several different formats you can use which is most likely your problem.
I tried single-quotes, too (I probably should have changed it back before posting the code).
And what it is echoing back to the client-side is the most confusing of all: 0. I am not sure if that is some kind of error for the json_encode method.
Re: Wondering how to format JSON in PHP
Posted: Tue Jun 29, 2010 10:09 pm
by Chris
Try force it as an object, It's probably an error in your config.
Code: Select all
json_encode($array, JSON_FORCE_OBJECT)
Re: Wondering how to format JSON in PHP
Posted: Tue Jun 29, 2010 10:18 pm
by Jackolantern
Chris wrote:Try force it as an object, It's probably an error in your config.
Code: Select all
json_encode($array, JSON_FORCE_OBJECT)
I tried that, too
As a side-note, I got it working through kind of a bootleg way by just making a fake JSON object and echoing it back to the client:
Code: Select all
echo '{ "monsterDmg": "'.$monsterDmg.'", "playerDmg":"'.$playerDmg.'"}';
The client recognized it that way. However, it would still be much more elegant to get it working through the function that is supposed to handle it, json_ecode. No idea what the problem is, though.
Re: Wondering how to format JSON in PHP
Posted: Tue Jun 29, 2010 10:28 pm
by Chris
Yeah, you could easily make your own function to do some basics of json. The most anoying thing would be multi dimensional arrays.
If it still didn't work I was going to suggest you try json_last_error().
http://www.php.net/manual/en/function.j ... -error.php
Maybe you can fix it this way.
here's a quick function, I have nothing to do XD:
Code: Select all
function json_encode_chris_style($array=NULL)
{
$str = "{";
$i = 0;
foreach( $array as $key => $value )
{
$str .= "\"$key\":$value" . ( ($i==0) || ($i==(count($array)-1)) ? '' : ", " );
$i++;
}
return $str . "}";
}
Edit: Major flaw in my function :O.
Re: Wondering how to format JSON in PHP
Posted: Tue Jun 29, 2010 10:30 pm
by Jackolantern
Nice, thank you! Looks like a good utility function
I will also have to check out that last error function, as I had not seen that before while reviewing JSON-related functions. Thanks again!