PHP json_decode返回null解析失败原因
2017-02-15 15:04
483 查看
在PHP5.4之前 json_decode函数有两个参数json_decode有两个参数,第一个是待解析的字符串,第二个是是否解析为Array
json_decode要求的字符串比较严格:
(1)使用UTF-8编码
(2)不能在最后元素有逗号
(3)不能使用单引号
(4)不能有\r,\t,如果有请替换
所以问题来了,不小心在返回的json字符串中返回了BOM头的不可见字符,某些编辑器默认会加上BOM头,如下处理才能正确解析json数据:
$result = json_decode(trim($contents,chr(239).chr(187).chr(191)),true);
print_r($result);
转载请注明地址: http://www.phpddt.com/php/json_decode-bom.html 尊重他人劳动成果就是尊重自己!
json_decode要求的字符串比较严格:
(1)使用UTF-8编码
(2)不能在最后元素有逗号
(3)不能使用单引号
(4)不能有\r,\t,如果有请替换
所以问题来了,不小心在返回的json字符串中返回了BOM头的不可见字符,某些编辑器默认会加上BOM头,如下处理才能正确解析json数据:
$result = json_decode(trim($contents,chr(239).chr(187).chr(191)),true);
print_r($result);
转载请注明地址: http://www.phpddt.com/php/json_decode-bom.html 尊重他人劳动成果就是尊重自己!
相关文章推荐
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- php使用json_decode解析json返回NULL
- PHP json_decode返回NULL原因及解决方法
- php使用json_decode解析json返回NULL
- PHP解码Json(json_decode)字符串返回NULL的原因及解决方法(转载)
- json_decode($str)返回NULL的一些原因
- PHP 获取JSON json_decode返回NULL解决办法
- PHP中file_get_contents函数获取带BOM的utf-8,然后json_decode() 返回null的问题
- PHP中使用了mcrypt_decrypt函数处理Json串后导致Json_decode返回null问题
- PHP中json_encode返回null的原因
- php json_decode 无法解析,返回值null
- php中json_decode无法解析原因
- php使用json_decode返回NULL
- php json_decode()返回null的解决方案
- PHP POST获取的JSON使用json_decode返回NULL
- Mysql中的数据用 json_decode 解析返回null的一则处理方法