欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

GBK的页面输出JSON格式的php函数

程序员文章站 2022-07-11 10:12:50
复制代码 代码如下:function tb_json_encode($value, $options = 0) { return json_encode(tb_json_c...
复制代码 代码如下:

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, $to)
{
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 ,$to);
}
return $m;
case ‘array':
foreach($m as $k => $v) {
$m[tb_json_convert_encoding($k, $from, $to)] = tb_json_convert_encoding($v, $from, $to);
}
return $m;
default:
}
return $m;
}