PHP接收JSON但无法解码它

时间:2013-05-20 16:15:48

标签: php json post decode

我在PHP中收到JSON代码,但如果我尝试解码它没有任何反应。

CODE:

$json = stripslashes($_POST['json']);
$output = json_decode($json);

当我将$json$output记录到控制台时:

$json值为:

{"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是:空字符串或null或undefined。我不知道。

控制台说:output is :

1 个答案:

答案 0 :(得分:3)

您的JSON使用"来表示字符串,但您的内容包含",例如

<div id="1" class="favorite">

因为您已使用stripslashes()删除了转义字符,所以字符串会提前结束,这会产生无效的JSON。

只需删除stripslashes()即可将这些字符转义。

$json = $_POST['json'];
$output = json_decode($json);

这就是PHP看到你的JSON的方式:

{
   "post": "{'newfavorite':'<div id=",
   1 // Error here - unexpected 1
   " class=" // unexpected string
   ...
}