在对小程序端(get)提交的数据在网页端进行显示的时候,使用PHP的json_decode()函数对已经经过JSON.stringify编译的数据转换为数组,结果返回null,使用json_last_error()输出error是Syntax error

解决方案:

$json = json_decode(html_entity_decode($cart_info));
var_dump($json);

结果:

【黄啊码】PHP:json_encode返回null_php

 当然,有的时候不是因为编码问题也有可能导致返回的数据解码是null,以下是json_decode($str)返回NULL的一些原因:
1.$str只能UTF-8编码
2.元素最后不能有逗号(与php的array不同)
3.元素不能使用单引号
4.元素值中间不能有空格和n,必须替换

以及json_last_error()输出错误代号标志:.

json_last_error错误msg对照表:
0 = JSON_ERROR_NONE
1 = JSON_ERROR_DEPTH
2 = JSON_ERROR_STATE_MISMATCH
3 = JSON_ERROR_CTRL_CHAR
4 = JSON_ERROR_SYNTAX
5 = JSON_ERROR_UTF8

具体参照php官网:​​https://www.php.net/manual/fr/function.json-last-error.php​