问题
I receive JSON code in PHP but if I try to decode it nothing happens.
CODE:
$json = stripslashes($_POST['json']);
$output = json_decode($json);
When I log $json
and $output
to the console:
$json
value is :
{"post":"{'newfavorite':'<div id="1" class="favorite"><sub class="minID">Id 1</sub><a href="http://www.youtube.com/watch?v=1PXQpWm_kq0">http://www.youtu</a><span onclick="movefavorite(1)"><img class="move" title="Move" src="icon/move.png"></span><span onclick="removefavorite(1)"><img class="delete" title="Delete" src="icon/del.png"></span></div>','username':'ifch0o'}"}
$output value is : empty string or null or undefined. I don't know.
Console says : output is :
回答1:
Your JSON uses "
to denote strings however your content contains "
e.g.
<div id="1" class="favorite">
Because you have removed the character escaping using stripslashes()
the strings are ending early and this is creating invalid JSON.
Simply remove stripslashes()
to keep those characters escaped.
$json = $_POST['json'];
$output = json_decode($json);
This is how PHP sees your JSON:
{
"post": "{'newfavorite':'<div id=",
1 // Error here - unexpected 1
" class=" // unexpected string
...
}
来源:https://stackoverflow.com/questions/16653786/php-receive-json-but-cant-decode-it