PHP Receive JSON but can't decode it

筅森魡賤 提交于 2019-12-24 00:34:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!