在对小程序端提交的数据在网页端进行显示的时候,使用PHP的json_decode()函数对已经经过JSON.stringify编译的数据转换为数组,结果返回null。
经常我们会使用 JSON.stringify() 保存某些数据,然后需要在php中读取,但在PHP接到这个参数的数据时,使用json_decode()却无法解析。
所以问题就是,为什么json_decode()解析JSON.stringify为什么为NULL。
解决方法:
当你使用 JSON stringify时,先在json_decode前使用 html_entity_decode。
代码示例:
$tempData = html_entity_decode($tempData); $cleanData = json_decode($tempData);
html_entity_decode(string,flags,character-set) 函数把 HTML 实体转换为字符,是 htmlentities() 函数的反函数。