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!
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()?
<?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
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
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.