In PHP, the file_get_contents function obtains UTF-8 with BOM, and then the json_decode () returns null.

Source: Internet
Author: User
In PHP, the file_get_contents function obtains UTF-8 with BOM, and then the json_decode () returns null. The problem is: how to parse json data in a file using php and return null.

{"a":1,"b":2,"x":[{"c":3},{"d":4},{"e":5}]}

The file_get_contents function is used to read files.

        $json = '{"a":1,"b":2,"x":[{"c":3},{"d":4},{"e":5}]}';        var_dump(json_decode($json));        var_dump(json_decode($json, true));

If you read the json string directly in php, there is no error. how can you read the json string from a file.

The following results are obtained:

PHP: json_decode-Manual

Http://www.php.net/manual/zh/function.json-decode.php

Php returns NULL using json_decode? Server O & M and Web architecture

Http://www.nginx.cn/337.html

Php json_decode null-toeasy-blog

Http://www.cnblogs.com/Toeasy/archive/2012/04/09/2439688.html

Json_decode () returns null-night-Yes-PHPChina-Powered by Discuz!
Http://bbs.phpchina.com/thread-267593-1-1.html

When obtaining the UTF-8 file content with BOM, use the file_get_contents function in PHP5.-wanglianghuaihua logs-NetEase Blog
Http://wanglianghuaihua.blog.163.com/blog/static/54251531201091915210555/

The key result is the following two. The reason for json_decode () parsing null is that the json file is in UTF-8 format with BOM.

The corrected code is as follows, which can be parsed properly.

$ 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 ); // If this parameter is set to TRUE, array instead of object is returned. Var_dump ($ dmList );

Display result:

View Code

{"Success": "true", "total": "4", "items": [{"id": "1", "c": "asdaEG ", "tb": "dm_suppliertype", "cn": "supplier type", "tips": "supplier type" },{ "id": "2", "c ": "adsafR", "tb": "suppliertype2", "cn": "supplier type 2", "tips": "supplier type" },{ "id ": "3", "c": "ada222", "tb": "suppliertype3", "cn": "supplier Type 3", "tips": "supplier type "}, {"id": "4", "c": "23 jetG", "tb": "suppliertype4", "cn": "supplier Type 4", "tips ": "Supplier type"}]} 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) "supplier type" ["tips"] => string (15) "Supplier type"} [1] => array (5) {["id"] => string (1) "2" ["c"] => string (6) "adsafR" ["tb"] => string (13) "suppliertype2" ["cn"] => string (16) "Supplier type 2" ["tips"] => string (15) "supplier type"} [2] => array (5) {["id"] => string (1) "3" ["c"] => string (6) "ada222" ["tb"] => string (13) "suppliertype3" ["cn"] => string (16) "supplier Type 3" ["tips"] => string (15) "Supplier type"} [3] => array (5) {["id"] => string (1) "4" ["c"] => string (6) "23 jetG" ["tb"] => string (13) "suppliertype4" ["cn"] => string (16) "supplier Type 4" ["tips"] => string (15) "supplier type "}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.