tags:

views:

58

answers:

4

I have the following JSON string, i try to decode with php json_decode but $postarray is always NULL, can't work out why this is?

Running on Debian 5.0 Linux php Client API version => 5.0.51a Json version 1.2.1

 $json = '{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}';

 $postarray = json_decode($json);
 print_r($postarray);

Thanks

+3  A: 

Try this:

<?php
$json = stripslashes('{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}');

$postarray = json_decode($json);
print_r($postarray);
Andrei Serdeliuc
+2  A: 

You should enclose it in double quotes.

Dennis Haarbrink
Well, that will bypass the problem, but that isn't the problem. The problem is that there are un-necessary backslashes (which I'm guessing are being added by [`magic_quotes_gpc`](http://us.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc)... Even if not, that doesn't help understand why it isn't working...
ircmaxell
@BoltClock Enclosing it with double quotes will make the "\" actually escape the double quotes inside the json string, therefore making it valid.
Andrei Serdeliuc
The single quotes make it stop working. Enclosing it in double quotes and it works.
Dennis Haarbrink
+1 from me, as it fixes the problem. @Dennis: You might explain why he should do that or what he could do instead (like replacing the backslashes).
Kau-Boy
+1 against the -1
NAVEED
@ Denniss, you don't deserve the vote-down because you solved it.+1 from me.
Amirouche Douda
@Kau-Boy: That's exactly why I did the -1. Not because this doesn't work, but because it doesn't fix the problem. Obviously the backslashes are there for a reason. The quesiton is what is that reason? Is it because of `magic_quotes`? Is it because the data was mis-escaped when being put into a database? Is it because `addslashes` was called right before? Sure, double quotes is the answer to the exact question given. However, it's not a general solution, and may not solve the OP's problem (since I have a feeling the string given was just a generalization of a bigger problem)...
ircmaxell
@ircmaxell: Usually when you recive JSON data from a request, the quotes are escaped. If you know about that useing double qoutes is a valid solution, even though stripslashes is a better one.
Kau-Boy
Huh? How is using double quotes a valid solution? The incomming JSON would be stored in a variable. So where do the dobule quotes go (since there's no string parsing going on which requires the escaped `"` characters as in the example provided)...
ircmaxell
+1  A: 

The string will not be parsed because it is enclosed in single quotes, so the backslashes are literal. If you remove them, use stripslashes, or enclose the string in double quotes, you should have no problems.

Luke
+5  A: 

The reason to escape double quotes (\") in a string, is if the string is double quoted.

Since you are escaping the double quotes, you should double (not single) quote your string, like this:

<?php
 $json = "{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}";

 $postarray = json_decode($json);
 print_r($postarray);
?>

Live Example

If you do want to single quote your string, then don't escape the double quotes, or use stripslashes() like Andrei suggested.

You can read about the four ways to specify a string in PHP, and the differences among them, here.

Peter Ajtai