PHP Json_encode processing Chinese characters as null service-side JSON Chinese character coding
The reason is that this character is GBK encoded, JSON can only handle UTF-8 encoding, so we have to transcode the data first
When using ExtJS to receive JSON results returned by the PHP server, it is a wonder that any characters (from SQL Server) are empty characters. Later read Json_encode's document only to know that it can only encode UTF-8 characters. The problem is that the returned character is GB2312, how it becomes UTF-8, and it is best to convert the entire array UTF-8. After searching, the following code is done:
Working with array encoding formats
function Td_iconv ($data, $charset _from, $charset _to) {
if (Strtolower ($charset _from) = = "gb2312") {
$charset _from = "GBK";
}
if (Strtolower ($charset _from) = = Strtolower ($charset _to)) {
return $data;
}
if (Is_array ($data)) {
foreach ($data as $k = = $v) {
if (Is_array ($v)) {
$data [$k] = Td_iconv ($v, $charset _from, $charset _to);
}else {
$data [$k] = is_string ($v)? Mb_convert_encoding ($v, $charset _to, $charset _from): $v;
}
}
}else {
if (is_string ($data))
$data = mb_convert_encoding ($data, $charset _to, $charset _from);
}
return $data;
}