Home  >  Article  >  Backend Development  >  PHP中file_get_contents函数获取带BOM的utf-8,然后json_decode() 返回null的问题

PHP中file_get_contents函数获取带BOM的utf-8,然后json_decode() 返回null的问题

WBOY
WBOYOriginal
2016-06-23 14:37:311140browse

  问题:用php读取文件中的json数据,怎么解析都是返回null。

{"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) "供应商类型"    }  }}

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn