PHP 获取JSON json_decode返回NULL解决办法

在用json_decode对JSON格式的字符串进行解码时为空,查阅资料才得知UTF-8的BOM头的原因

json_decode要求的字符串比较严格:

(1)使用UTF-8编码

(2)不能在最后元素有逗号

(3)不能使用单引号

(4)不能有\\r,\\t,如果有请替换

解决方法1:

$info = json_decode(trim($info,chr(239).chr(187).chr(191)),true);

解决方法2:

匹配是否有BOM头,存在则截取

if(preg_match('/^\xEF\xBB\xBF/',$info )){
    $json_data = substr($info,3);
}
$info = json_decode(trim($info ),true);


© 版权声明
THE END