Question about json_decode garbled and NULL _ javascript skills

Source: Internet
Author: User
Jsondecode contains Chinese garbled characters and returns null. Many friends have encountered this problem. how can this problem be solved? The solutions below are shared on the script home website. for details, refer to the following.

Users who write interfaces should frequently encounter data format conversion. at this time, two essential functions are json_encode () and json_decode ().

These two functions have many main issues. here I will talk about json_decode ().

Json_decode (): decodes a JSON string, accepts a JSON string, and converts it to a PHP variable.

(1) after the data is converted into an array, the print will display NUll:

One of the reasons is that json_decode only supports UTF-8.

Iconv ('gbk', 'utf-8', $ result_string); use the iconv function to convert the gbk encoding format of the data to the UTF-8 encoding format to be output, if the original data is in UTF-8 format, skip this step. otherwise, garbled characters will appear.

Cause 2: The json string must contain double quotation marks.

Str_replace ("'",' "', $ result_string); // replace the single quotes in json data with double quotes.

Cause 3: there cannot be redundant commas, for example, [,]

Replace with regular expressions, preg_replace ('/, \ s * ([\]}])/M',' $ 1', $ result_string );

(2) garbled characters are displayed after data is converted to an array or json format:

Urlencode () and urldecode () are used at this time ()

The following is my code, which has been tested and valid:

If (file_exists ($ result ['SAVE _ path']) {$ contents = file_get_contents ($ result ['SAVE _ path']); // write the content of a file. the file is in UTF-8 format and contains json data // $ getcontent = iconv ("gbk", "UTF-8 // ignore ", $ contents); // if the file is originally in UTF-8 format, you do not need to convert $ getcontent = str_replace ("'",' "', $ contents ); // replace a single quote with a double quote preg_replace ('/, \ s * ([\]}])/M',' $ 1', $ getcontent ); // remove unnecessary commas $ new_array = array (); $ new_array = json_decode ($ getcontent, true); $ res = array (); foreach ($ new_array as $ key => $ val) {foreach ($ new_array [$ key] ['items '] as $ k => $ v) {if ($ k <$ row) {$ res [$ k] ['position'] = $ v ['position']; $ res [$ k] ['distance '] = $ v ['distance']; $ res [$ k] ['title'] = urlencode ($ v ['title']); $ res [$ k ['vicinity '] = urlencode ($ v ['vicinity']) ;}} if ($ res) {$ new_res ['items '] = $ res;} else {$ new_res ['items'] = "" ;}echo urldecode (json_encode ($ new_res ));}

The above content is a small series of questions about json_decode garbled characters and NULL. I hope to help you.

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.