php json_decode 函数解析 json 结果为 NULL,使用json_last_error()打印提示语法错误。
可以对字符串进行转utf-8编码。
或进行json数据格式化,如果格式正确参考解决方式二。
解决方式一:
php
- htmlspecialchars_decode($str);
解决方式二:
使用var_dump()打印json字符串,会发现json字符串前面会有3个空格,进行过滤BOM头。
php
- function removeBOM($str) {
- if (0 === strpos(bin2hex($str), 'efbbbf')) {
- return substr($data, 3);
- }
- return $data;
- }
- removeBOM($str);
解决方式三:
如果有unicode编码先进行解码,如果有反斜杠就去掉。
php
- stripslashes(decodeUnicode($str));