PHP JSON Chinese encoding for null solution _php Tips

Source: Internet
Author: User

Today, using the Json_encode function, we find that Chinese has become null.

Reason analysis: Use the Json_encode function should use the UTF-8 encoding, my page is GBK.

Workaround: Use the Iconv (' gbk ', ' UTF8 ') function before the Json_encode function. Function

function Gbk2utf8 ($data)
{
 if (Is_array ($data))
  {return
    array_map (' Gbk2utf8 ', $data);
  }
 Return Iconv (' GBK ', ' utf-8 ', $data);
}

Here's another way to find out.

PHP from the database to get the field is Chinese, want to use Json_encode () back to the foreground, but Json_encode encoded in Chinese is null.

Use the Json_encode () built-in function (Php > 5.2) in PHP to use the data in PHP to communicate well with other languages and use it.

The function of this function is to convert a numeric value into a JSON data storage format.

<?php
$arr = array
    (
     ' Name ' => ' Shia ',
     ' age ' =>20
    );

$jsonencode = Json_encode ($arr);
echo $jsonencode;
? >

The results of the program operation are as follows:
{' Name ': null, ' Age ': 20}
Json_encode function in Chinese is encoded into null, Google a bit, very simple, in order to close with the front-end, Json only support utf-8 encoding, I think the front-end Javascript is also the reason for Utf-8.

<?php
$array = array
	('
	 	title ' =>iconv (' gb2312 ', ' utf-8 ', ' Here is the Chinese title '),
		' body ' => ' abcd ... '
	);

echo Json_encode ($array);
? >

The results of this program are:
{"title": "\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898", "Body": "ABCD ..."}
All Chinese in the array are missing or appear \u2353 after Json_encode.
The solution is to use the UrlEncode () function to process the following, Json_encode all the contents of the array in UrlEncode (), and then convert the Json_encode () to the JSON string, and then the UrlDecode () Turn the encoded Chinese back in.

<?php/************************************************************** * * Use a specific function to handle all elements in the array * @param string & 
 amp; $array the string to be processed * @param string $function The function to be executed * @return Boolean $apply _to_keys_also is also applied to the key * @access public * *************************************************************/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, $app
    LY_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]);
}}} $recursive _counter--; }
 
/**************************************************** Convert an array to a JSON string (compatible with Chinese) * @param array $array arrays to convert * @return string converted JSON String * @access public * /function JSON ($array) {arrayrecursive ($array, '
	UrlEncode ', true);
	$json = Json_encode ($array);
Return UrlDecode ($json);


$array = Array (' Name ' => ' Shia ', ' age ' =>20); echo JSON ($array);?>

The

was successful this time, and the results were as follows:
{"Name": "Shia", "Age": "}

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.