PHP接收JSON但无法对其进行解码 [英] PHP Receive JSON but can't decode it
问题描述
我在PHP中收到JSON代码,但是如果我尝试对其进行解码,则不会发生任何事情.
I receive JSON code in PHP but if I try to decode it nothing happens.
代码:
$json = stripslashes($_POST['json']);
$output = json_decode($json);
当我将$json
和$output
登录到控制台时:
When I log $json
and $output
to the console:
$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的值是:空字符串或null或未定义.我不知道.
$output value is : empty string or null or undefined. I don't know.
控制台说:output is :
推荐答案
您的JSON使用"
表示字符串,但是您的内容包含"
例如
Your JSON uses "
to denote strings however your content contains "
e.g.
<div id="1" class="favorite">
因为您已经删除了使用stripslashes()
进行转义的字符,所以字符串提早结束了,这将创建无效的JSON.
Because you have removed the character escaping using stripslashes()
the strings are ending early and this is creating invalid JSON.
只需删除stripslashes()
即可保留这些字符.
Simply remove stripslashes()
to keep those characters escaped.
$json = $_POST['json'];
$output = json_decode($json);
这是PHP看到您的JSON的方式:
This is how PHP sees your JSON:
{
"post": "{'newfavorite':'<div id=",
1 // Error here - unexpected 1
" class=" // unexpected string
...
}
这篇关于PHP接收JSON但无法对其进行解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!