PHP中file_get_contents函数获取带BOM的utf-8,然后json_decode() 返回null的问题
2013-01-31 21:18
891 查看
问题:用php读取文件中的json数据,怎么解析都是返回null。
读取文件,使用了file_get_contents函数。
如果直接在php中读取json字符串,是没有错的,怎么从文件读取就错了呢。
搜索得以下结果:
PHP: json_decode - Manual
http://www.php.net/manual/zh/function.json-decode.php
php使用json_decode返回NULL – 服务器运维与Web架构
http://www.nginx.cn/337.html
php json_decode null - toeasy - 博客园
http://www.cnblogs.com/Toeasy/archive/2012/04/09/2439688.html
json_decode() 得到null-夜色-Yes-PHPChina - Powered by Discuz!
http://bbs.phpchina.com/thread-267593-1-1.html
PHP5中file_get_contents函数获取带BOM的utf-8文件内容时需注意 - wanglianghuaihua的日志 - 网易博客
http://wanglianghuaihua.blog.163.com/blog/static/54251531201091915210555/
关键结果在是后面两个。造成json_decode() 解析null的原因是,json文件是UTF-8格式,带有BOM。
修正后代码如下,即可正常解析。
显示结果:
View Code
{"a":1,"b":2,"x":[{"c":3},{"d":4},{"e":5}]}
读取文件,使用了file_get_contents函数。
$json = '{"a":1,"b":2,"x":[{"c":3},{"d":4},{"e":5}]}'; var_dump(json_decode($json)); var_dump(json_decode($json, true));
如果直接在php中读取json字符串,是没有错的,怎么从文件读取就错了呢。
搜索得以下结果:
PHP: json_decode - Manual
http://www.php.net/manual/zh/function.json-decode.php
php使用json_decode返回NULL – 服务器运维与Web架构
http://www.nginx.cn/337.html
php json_decode null - toeasy - 博客园
http://www.cnblogs.com/Toeasy/archive/2012/04/09/2439688.html
json_decode() 得到null-夜色-Yes-PHPChina - Powered by Discuz!
http://bbs.phpchina.com/thread-267593-1-1.html
PHP5中file_get_contents函数获取带BOM的utf-8文件内容时需注意 - wanglianghuaihua的日志 - 网易博客
http://wanglianghuaihua.blog.163.com/blog/static/54251531201091915210555/
关键结果在是后面两个。造成json_decode() 解析null的原因是,json文件是UTF-8格式,带有BOM。
修正后代码如下,即可正常解析。
$dmText = file_get_contents( AROOT .'data' . DS . 'DMType.json.php'); if(preg_match('/^\xEF\xBB\xBF/',$dmText)) { $dmText = substr($dmText,3); } //trim $dmText = t($dmText); echo $dmText; /* create array list from comments */ $dmList = json_decode($dmText,true); //当该参数为 TRUE 时,将返回 array 而非 object 。 var_dump($dmList);
显示结果:
View Code
{ "success": "true", "total":"4", "items": [ {"id":"1","c":"asdaEG","tb": "dm_suppliertype", "cn": "供应商类型","tips":"供应商类型"}, {"id":"2","c":"adsafR","tb": "suppliertype2", "cn": "供应商类型2","tips":"供应商类型"}, {"id":"3","c":"ada222","tb": "suppliertype3", "cn": "供应商类型3","tips":"供应商类型"}, {"id":"4","c":"23jetG","tb": "suppliertype4", "cn": "供应商类型4","tips":"供应商类型"} ] }array(3) { ["success"]=> string(4) "true" ["total"]=> string(1) "4" ["items"]=> array(4) { [0]=> array(5) { ["id"]=> string(1) "1" ["c"]=> string(6) "asdaEG" ["tb"]=> string(15) "dm_suppliertype" ["cn"]=> string(15) "供应商类型" ["tips"]=> string(15) "供应商类型" } [1]=> array(5) { ["id"]=> string(1) "2" ["c"]=> string(6) "adsafR" ["tb"]=> string(13) "suppliertype2" ["cn"]=> string(16) "供应商类型2" ["tips"]=> string(15) "供应商类型" } [2]=> array(5) { ["id"]=> string(1) "3" ["c"]=> string(6) "ada222" ["tb"]=> string(13) "suppliertype3" ["cn"]=> string(16) "供应商类型3" ["tips"]=> string(15) "供应商类型" } [3]=> array(5) { ["id"]=> string(1) "4" ["c"]=> string(6) "23jetG" ["tb"]=> string(13) "suppliertype4" ["cn"]=> string(16) "供应商类型4" ["tips"]=> string(15) "供应商类型" } } }
相关文章推荐
- PHP中file_get_contents函数获取带BOM的utf-8,然后json_decode() 返回null的问题
- PHP中file_get_contents函数获取带BOM的utf-8,然后json_decode() 返回null的问题
- php -- PHP5中file_get_contents函数获取带BOM的utf-8文件内容
- 带BOM的utf-8,用json_decode() 返回null的问题 --- 超过3个bom字符
- PHP5中file_get_contents函数获取带BOM的utf-8文件内容时需注意
- PHP POST获取的JSON使用json_decode返回NULL
- 解决PHP下json_decode返回NULL的问题。
- PHP 获取JSON json_decode返回NULL解决办法
- php的file_get_contents无法https协议下获取json的问题
- PHP json_decode的坑 仅仅支持utf-8编码的字符,gbk编码会返回null
- PHP中使用了mcrypt_decrypt函数处理Json串后导致Json_decode返回null问题
- PHP POST获取的JSON使用json_decode返回NULL
- PHP5中file_get_contents函数获取带BOM的utf-8文件内容时需注意
- 解决json插入mysql后再读出使用json_decode()返回null问题
- 执行、获取远程代码返回:file_get_contents 超时处理的问题详解
- 关于php服务器代理上网的时候使用file_get_contents函数取不到内容的问题的解决方案
- PHP知识点 - json_decode 返回 NULL,PHP 的 JSON 相关函数
- php json_decode()返回null的解决方案
- php解决aes,base64后的json字符串json_decode为null的问题
- PHP json_decode返回null解析失败原因