JSON issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

JSON issue

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: JSON issue

Post by Benjamin »

Sounds like magic_quotes are on.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: JSON issue

Post by social_experiment »

Yeah i disabled them then it seemed to work ok; (well after i saw that stripslashes() did the trick)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: JSON issue

Post by pickle »

Not sure if it matters, but technically that's not valid JSON. Property key values shouldn't be quoted.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: JSON issue

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: JSON issue

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: JSON issue

Post by social_experiment »

:) no worries
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply