Use the json_decode function in php to convert json into an array and return null.

Source: Internet
Author: User
Obtain a data remotely in the json format, but return null when converting json_decode to an array. I don't know why, the json data does exist to obtain a data remotely, in the form of json, but returns null when json_decode is converted to an array. I don't know why, json data does exist.

Reply content:

Obtain a data remotely in the json format, but return null when converting json_decode to an array. I don't know why, json data does exist.

Use json_last_error to view the error

When the returned value is null, it may be because the first input parameter cannot be decode, for example, some non-json_encode strings, or the data depth of encode exceeds the permitted range. Check whether the data you obtained is a standard json_encode string. Sometimes there are double quotation marks in the data, which may cause data errors. You 'd better publish the string to be processed here

Add the second parametertrueLet's see.

Print_r (json_decode ($ json, true); // where true indicates that array is returned rather than object.

Json_decode ($ arr, true); is converted to an array, without adding the second parameter is converted to an object.

You can check whether it is in standard JSON format: http://www.bejson.com/

The second reason is:
Note: BOM header output is not allowed.
Before PHP5.4, The json_decode function has two parameters: json_decode, the first one is the string to be parsed, and the second one is whether it is parsed as an Array.

Json_decode requires strict strings:
(1) Use UTF-8 Encoding
(2) The last element cannot contain commas.
(3) single quotes are not allowed.
(4) \ r, \ t are not allowed. If yes, replace
Solution 1:
The problem is that the invisible characters of the BOM header are returned in the returned json string. Some editors will add the BOM header by default. The json data can be correctly parsed only after the following processing:


  

Method 2: transcode Chinese before converting to json

Function arrayRecursive (& $ 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_k Ey = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}}$ recursive_counter --;} /*************************************** * ************************ convert an array to a JSON string (compatible with Chinese characters) * @ param array $ array the array to be converted * @ return string the converted json string * @ access public ***************** **************************************** * ***/function JSON ($ array) {arrayRecursive ($ array, 'urlencode', true); $ json = json_encode ($ array); return urldecode ($ json);} echo JSON ($ user );

For details, refer to my logs:http://my.oschina.net/rain21/blog/384549

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.