Before encode, convert it to UTF-8. After decode, convert it back to gbk:
Before encode, convert it to UTF-8. After decode, convert it back to gbk:
The Code is as follows:
Function tb_json_encode ($ value, $ options = 0)
{
Return json_encode (tb_json_convert_encoding ($ value, "GBK", "UTF-8 ″));
}
Function tb_json_decode ($ str, $ assoc = false, $ depth = 512)
{
Return tb_json_convert_encoding (json_decode ($ str, $ assoc), "UTF-8", "GBK ");
}
Function tb_json_convert_encoding ($ m, $ from, $)
{
Switch (gettype ($ m )){
Case 'integer ':
Case 'boolean ':
Case 'float ':
Case 'double ':
Case 'null ':
Return $ m;
Case 'string ':
Return mb_convert_encoding ($ m, $ to, $ from );
Case 'object ':
$ Vars = array_keys (get_object_vars ($ m ));
Foreach ($ vars as $ key ){
$ M-> $ key = tb_json_convert_encoding ($ m-> $ key, $ from, $ );
}
Return $ m;
Case 'array ':
Foreach ($ m as $ k => $ v ){
$ M [tb_json_convert_encoding ($ k, $ from, $ to)] = tb_json_convert_encoding ($ v, $ from, $ );
}
Return $ m;
Default:
}
Return $ m;
}