Json parsing solution-php Tutorial

Source: Internet
Author: User
Json parsing: I have solved the problem that json_encode cannot pass Chinese characters. in the case of gbk encoding, how can I use the json_decode () function to parse it? PHPcodefunctionarrayRecursive (& amp; $ array, $ function, $ apply_to_keys_alsofalse) {static $ r json parsing
Q: I have solved the problem that json_encode cannot pass Chinese characters in gbk encoding. but how can I use the json_decode () function to parse it?
PHP code
  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); $ json = urldecode ($ json ); $ json = str_replace ("\" false \ "", "false", $ json); $ json = str_replace ("\" true \ "", "true ", $ json); return $ json;} function get_goodbook () {$ SQL = "select * from 'Wiki _ doc "'; $ data = $ this-> db-> getAll ($ SQL); return JSON ($ data );}


------ Solution --------------------
Json_encode can be used to transmit Chinese characters. iconv is used to input logs.
------ Solution --------------------
You encoded the content as a url. how do you know you need to decode the content after the other party (such as js) obtains it?

You should perform iconv ('gbk', 'utf-8', $ value) encoding conversion in the arrayRecursive function.
------ Solution --------------------
You can encapsulate it yourself.
PHP code
$ Ar = array ('Here is GBK encoded data', array ('This is a test', 'this is a test ',),); echo $ s = json_encode (array_map ('gb2utf', $ ar); print_r (array_map ('utf2gb', json_decode ($ s); function gb2utf ($ v) {if (! Is_array ($ v) return iconv ('gbk', 'utf-8', $ v); foreach ($ v as & $ t) $ t = gb2utf ($ t); return $ v;} function utf2gb ($ v) {if (! Is_array ($ v) return iconv ('utf-8', 'gbk', $ v); foreach ($ v as & $ t) $ t = utf2gb ($ t); return $ v ;}
------ Solution --------------------
First, convert the array to UTF-8 to encode Chinese characters. there is no need to write many other codes.

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.