【发布时间】:2013-05-15 06:18:36
【问题描述】:
我在 PHP 中收到 JSON 代码,但如果我尝试对其进行解码,则没有任何反应。
代码:
$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 值为:空字符串或 null 或未定义。我不知道。
控制台说:output is :
【问题讨论】:
-
JSON 无效;你有不正确的嵌套引号
-
json 输入无效。 PHP 无法验证它,任何其他语言的任何其他 JSON 解码器也无法验证。将您的代码粘贴到 this one 之类的验证器中以供证明。
-
这里的教训是使用适当的 json 编码器来生成 json,而不是自己构建它。您需要修复输入;在 PHP 端你无能为力来修复它。 (如果这个 json 来自第三方,那么你需要抱怨他们不是很好)
-
首先验证您的 json。您可以使用以下网站:jsonlint.com 来确保您的 json 有效。
-
如果斜杠没有被去掉,这个 JSON 是有效的...