PHP JSON_DECODE/JSON_ENCODE: The Chinese content is NULL or garbled

Source: Internet
Author: User

Example

The code is as follows: Copy code

<? Php
$ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5 }';

Var_dump (json_decode ($ json ));
Var_dump (json_decode ($ json, true ));

?>

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)
}

Completely correct, no problem, so we test Chinese

The code is as follows: Copy code

<? Php
$ Json = '{"a": ""}';

Var_dump (json_decode ($ json ));

?>

Result

{"Text": null, "status": 1}

Later, we learned from the php manual that json_encode and json_decode only support UTF-8 encoded characters, and the GBK characters must be converted to json, so that we can do it well.

Convert an encoding

The code is as follows: Copy code

/*
The string GBK is transcoded to a UTF-8, and the number is converted to a number.
*/
Function ct2 ($ s ){
If (is_numeric ($ s )){
Return intval ($ s );
} Else {
Return iconv ("GBK", "UTF-8", $ s );
    }
}
/*
Batch processing gbk-> UTF-8
*/
Function icon_to_utf8 ($ s ){

If (is_array ($ s )){
Foreach ($ s as $ key => $ val ){
$ S [$ key] = icon_to_utf8 ($ val );
    }
} Else {
$ S = ct2 ($ s );
  }
Return $ s;

}

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, use urldecode () to convert the encoded Chinese characters back.

A function is written.

The code is as follows: Copy code

/*************************************** ***********************
*
* Use a specific function to process all elements in the array
* @ Param string & $ array the string to be processed
* @ Param string $ function the function to be executed
* @ Return boolean $ apply_to_keys_also whether it is also applied to the key
* @ Access public
*
**************************************** *********************/
Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false)
{
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]);
            }
        }
    }
}

/*************************************** ***********************
*
* 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 );
}

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.