Page 1 of 1

JSON issue

Posted: Thu Nov 03, 2011 2:53 am
by social_experiment
I'm trying to use json_decode() on a value i receive from an ajax call. The information i receive from the call looks correct ( "info":"58" ) but when i use json_decode() to display it to the browser, it displays as {\"info\":\"58\"}. From phpinfo i determined that json support is enabled, the version is 1.2.1 and the php version is 5.2.5. What's the correct method to use json_decode()?

Code: Select all

<?php
 // if i use the following code i don't get any value printed to the browser
 // using the web console in FireFox, the value of $_GET['value'] is
 // %22info%22:%22abc%22

 $decoded = json_decode($_GET['value']);
 print_r($decoded);
?>
A final point, if i call json_last_error() i get an undefined function fatal error

Edit
When i stripped the slahses from $_GET['value'] is seems to solve the problem.

Re: JSON issue

Posted: Thu Nov 03, 2011 4:38 am
by Benjamin
Sounds like magic_quotes are on.

Re: JSON issue

Posted: Thu Nov 03, 2011 5:52 am
by social_experiment
Yeah i disabled them then it seemed to work ok; (well after i saw that stripslashes() did the trick)

Re: JSON issue

Posted: Thu Nov 03, 2011 3:41 pm
by pickle
Not sure if it matters, but technically that's not valid JSON. Property key values shouldn't be quoted.

Re: JSON issue

Posted: Thu Nov 03, 2011 4:26 pm
by social_experiment
I see the manual says the following about the quotes issue
The Manual wrote: // the following strings are valid JavaScript but not valid JSON
// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null
Does this mean that the output i receive is valid javascript but not valid JSON or does the given example qualify as valid javascript but not valid JSON?

Regardless of the answer above, what can i do to get correction JSON values? The value is created by javascript so i would guess that the answer is related to that

Re: JSON issue

Posted: Thu Nov 03, 2011 4:36 pm
by pickle
Hmmm, I just ran what I thought was bad JSON through the validator & it came up valid. JSON I thought was valid turned up broken. Looks like I'm wrong here. Sorry.

Re: JSON issue

Posted: Thu Nov 03, 2011 4:48 pm
by social_experiment
:) no worries