PHP json_decode返回null解析失败原因
2015-03-23 14:20
666 查看
在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);
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);
相关文章推荐
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- PHP json_decode返回null解析失败原因
- PHP解码Json(json_decode)字符串返回NULL的原因及解决方法(转载)
- php使用json_decode解析json返回NULL
- php使用json_decode解析json返回NULL
- PHP json_decode返回NULL原因及解决方法
- php使用json_decode返回NULL
- Mysql中的数据用 json_decode 解析返回null的一则处理方法
- PHP 获取JSON json_decode返回NULL解决办法
- PHP json_decode 函数解析 json 结果为 NULL 的解决方法
- PHP中使用了mcrypt_decrypt函数处理Json串后导致Json_decode返回null问题
- PHP中json_encode返回null的原因
- php使用json_decode返回NULL
- PHP json_decode 函数解析 json 结果为 NULL 的解决方法
- json_encode不对中文进行转码、json_decode解析json字符串返回NULL的处理方法
- php使用json_decode返回NULL
- PHP中file_get_contents函数获取带BOM的utf-8,然后json_decode() 返回null的问题