views:

3337

answers:

6

There is a strange behaviour with json_encode and json_decode and I can't find a solution:

My php application calls a php web service. The webservice returns json that looks like this:

var_dump($foo):
string(62) "{"action":"set","user":"123123123123","status":"OK"}"

now I like to decode the json in my application:

$data = json_decode($foo, true)

but it returns NULL:

var_dump($data):
NULL

I use php 5. The Content-Type of the response from the webservice: "text/html; charset=utf-8" (also tried to use "application/json; charset=utf-8")

What could be the reason?

A: 

Try this

$foo = utf8_encode($foo);
$data = json_decode($foo, true);
Ólafur Waage
Hi Olafur,utf8_encode writes some strange characters to the beginning of the string.utf8_decode writes a ? to the beginning of the string.This is just a workaround, but it works. I'm sure there is a better way:$foo = utf8_decode($result->data);json_decode(str_replace("?", "", $foo), true);
schouk
+2  A: 
"{"action":"set","user":"123123123123","status":"OK"}"

This little apostrophe in the beginning - what is it? First symbol after the doublequote.

Kuroki Kaze
I can't find a little apostrophe. First symbol after the doublequote is a curly bracket.
schouk
+7  A: 

EDIT: Just did some quick inspection of the string provided by the OP. The small "character" in front of the curly brace is a UTF-8 B(yte) O(rder) M(ark) 0xEF 0xBB 0xBF. I don't know why this byte sequence is displayed as  here.

Essentially the system you aquire the data from sends it encoded in UTF-8 with a BOM preceding the data. You should remove the first three bytes from the string before you throw it into json_decode() (a substr($string, 3) will do).

string(62) "{"action":"set","user":"123123123123","status":"OK"}"
            ^
            |
            This is the UTF-8 BOM

As Kuroki Kaze discovered, this character surely is the reason why json_decode fails. The string in its given form is not correctly a JSON formated structure (see RFC 4627)

Stefan Gehrig
I don't think so. This is a curly bracket. I think every JSON structure should begin with a curly bracket.
schouk
No not the curly brace - there is a character just before the curly brace.
Stefan Gehrig
I think SO's formatting is trying to make this a hard question to answer. :-p
Andrew
-> ok, Notepad++ lets me see this character. My FF3 doesn't display it...Many Thanks! :)
schouk
+2  A: 

Well, i had a similar issue and the problems was the PHP magic quotes in the server... here is was my solution:

if(get_magic_quotes_gpc()){
  $d = stripslashes($_POST['param']);
}else{
  $d = $_POST['param'];
}
$d = json_decode($d,true);
Pablo
Thanks a lot Pablo for your answer!! you saved me a lot of time!!
Alejandra
+1  A: 

Print the last json error when debugging.

json_decode( $so, true, 9 )
$json_errors = array(
    JSON_ERROR_NONE => 'No error has occurred',
    JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
    JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
    JSON_ERROR_SYNTAX => 'Syntax error',
);
 echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
Also use the json.stringify() function to double check your JSON syntax.

LarryBattle
A: 

i had a similar problem, got it to work after adding '' (single quotes) around the json_encode string. Following from my js file:

var myJsVar  = <?php echo json_encode($var); ?> ;    -------> NOT WORKING  
var myJsVar = '<?php echo json_encode($var); ?>' ;    -------> WORKING

just thought of posting it in case someone stumbles upon this post like me :)

harry