Wondering how to format JSON in PHP

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Wondering how to format JSON in PHP

Post 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.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Wondering how to format JSON in PHP

Post 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.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Wondering how to format JSON in PHP

Post 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.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Wondering how to format JSON in PHP

Post 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) 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Wondering how to format JSON in PHP

Post 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.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Wondering how to format JSON in PHP

Post 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.
Last edited by Chris on Wed Jun 30, 2010 7:05 am, edited 2 times in total.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Wondering how to format JSON in PHP

Post 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!
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”