PHP get JSON json_decode return null workaround

Source: Internet
Author: User

In the use of Json_decode JSON format string decoding is empty, page blank Ah, the whole half-day check here to check there, ask colleagues are useless.

Bing search today, the problem solved, originally there is a BOM head output , prawn solution is as follows:

1), Json_decode ($json, Boolean): $json is a JSON-formatted character, the second parameter is true when the conversion is an array, and the default false indicates

Convert to an object.

2), Json_decode Required string comparison is strict:

(1) Use UTF-8 Code (2) cannot have comma (3) in the last element cannot use single quotation mark (4) cannot have \r,\t, if please replace
Solution One:
So here's the problem. Returns the invisible character of the BOM header in the returned JSON string, some editors default to the BOM header, and the following processing is required to correctly parse the JSON data:
<?php$info = json_decode(trim($info,chr(239).chr(187).chr(191)),true);

方法二:在转为json之前对中文进行转码
functionArrayrecursive (&$array,$function,$apply _to_keys_also=false){    Static $recursive _counter= 0; if(++$recursive _counter>1000 ) {         die(' Possible deep recursion attack '); }    foreach($array  as $key=$value) {        if(Is_array($value) {arrayrecursive ($array[$key],$function,$apply _to_keys_also); } Else {            $array[$key] =$function($value); }        if($apply _to_keys_also&&is_string($key)) {            $new _key=$function($key); if($new _key!=$key) {                $array[$new _key] =$array[$key]; unset($array[$key]); }        }    }    $recursive _counter--;}/************************************************************** * * Convert an array to a JSON string (Chinese language compatible) * @param array $array to be converted * @return string converted to JSON string * @access public * *************************************************************/functionJSON ($array) {arrayrecursive ($array, ' UrlEncode ',true); $json= Json_encode ($array); return UrlDecode($json);}EchoJSON ($user);
View Code

Reprint Address: https://segmentfault.com/q/1010000002991973

There is also a description of the details: http://www.cnblogs.com/xcxc/p/3729207.html

PHP get JSON json_decode return null workaround

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.