PHPJSON_DECODE/JSON_ENCODE: The Chinese content is NULL or garbled

Source: Internet
Author: User
When using json data, many of my friends may use the JSON_DECODEJSON_ENCODE function of php to process Chinese content, which may cause NULL or garbled characters. what can I introduce to you below?

Many friends may encounter NULL or garbled characters when using the JSON_DECODE/JSON_ENCODE function of php to process Chinese content when using json data, next I will introduce to you why this problem occurs, for example:

  1. $ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5 }';
  2. Var_dump (json_decode ($ json ));
  3. Var_dump (json_decode ($ json, true ));
  4. ?>

Output result

Object (stdClass) #1 (5) {["a"] => int (1) ["B"] => int (2) ["c"] => int (3) ["d"] => int (4) ["e"] => int (5)} array (5) {["a"] => int (1) ["B"] => int (2) ["c"] => int (3) ["d"] => int (4) ["e"] => int (5 )}

If it is completely correct and there is no problem, we test the Chinese language and the code is as follows:

  1. $ Json = '{"a": ""}';
  2. Var_dump (json_decode ($ json ));
  3. ?>

Result: {"text": null, "status": 1}. it was obtained from the php manual that json_encode and json_decode only support UTF-8 characters, the GBK character must be converted to json, so that we can easily convert it.

The code for converting an encoding is as follows:

  1. /*
  2. The string GBK is transcoded to a UTF-8, and the number is converted to a number.
  3. */
  4. Function ct2 ($ s ){
  5. If (is_numeric ($ s )){
  6. Return intval ($ s );
  7. } Else {
  8. Return iconv ("GBK", "UTF-8", $ s );
  9. }
  10. }
  11. /*
  12. Batch processing gbk-> UTF-8
  13. */
  14. Function icon_to_utf8 ($ s ){
  15. If (is_array ($ s )){
  16. Foreach ($ s as $ key => $ val ){
  17. $ S [$ key] = icon_to_utf8 ($ val );
  18. }
  19. } Else {
  20. $ S = ct2 ($ s );
  21. }
  22. Return $ s;
  23. }
  24. Echo json_encode (icon_to_utf8 ("Xiamen "));

In this case, there are still some problems. later I found a method to process all the content in the array using urlencode () before json_encode, and then convert it to a json string using json_encode, finally, urldecode () is used to convert the encoded Chinese characters and write a function:

  1. /*************************************** ***********************
  2. *
  3. * Use a specific function to process all elements in the array
  4. * @ Param string & $ array the string to be processed
  5. * @ Param string $ function the function to be executed
  6. * @ Return boolean $ apply_to_keys_also whether it is also applied to the key
  7. * @ Access public
  8. *
  9. **************************************** *********************/
  10. Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false)
  11. {
  12. Foreach ($ array as $ key => $ value ){
  13. If (is_array ($ value )){
  14. ArrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also );
  15. } Else {
  16. $ Array [$ key] = $ function ($ value );
  17. }
  18. If ($ apply_to_keys_also & is_string ($ key )){
  19. $ New_key = $ function ($ key );
  20. If ($ new_key! = $ Key ){
  21. $ Array [$ new_key] = $ array [$ key];
  22. Unset ($ array [$ key]);
  23. }
  24. }
  25. }
  26. }
  27. /*************************************** ***********************
  28. *
  29. * Convert an array to a JSON string (compatible with Chinese characters)
  30. * @ Param array $ array the array to be converted
  31. * @ Return string the converted json string
  32. * @ Access public
  33. *
  34. **************************************** *********************/
  35. Function JSON ($ array ){
  36. ArrayRecursive ($ array, 'urlencode', true );
  37. $ Json = json_encode ($ array );
  38. Return urldecode ($ json );
  39. }

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.