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

GBK页面输出JSON的php代码

程序员文章站 2022-05-01 13:39:54
...
  1. function tb_json_encode($value, $options = 0)

  2. {
  3. return json_encode(tb_json_convert_encoding($value, “GBK”, “UTF-8″));
  4. }
  5. function tb_json_decode($str, $assoc = false, $depth = 512)

  6. {
  7. return tb_json_convert_encoding(json_decode($str, $assoc), “UTF-8″, “GBK”);
  8. }
  9. function tb_json_convert_encoding($m, $from, $to)

  10. {
  11. switch(gettype($m)) {
  12. case ‘integer’:
  13. case ‘boolean’:
  14. case ‘float’:
  15. case ‘double’:
  16. case ‘NULL’:
  17. return $m;
  18. case ’string’:

  19. return mb_convert_encoding($m, $to, $from);
  20. case ‘object’:
  21. $vars = array_keys(get_object_vars($m));
  22. foreach($vars as $key) {
  23. $m->$key = tb_json_convert_encoding($m->$key, $from ,$to);
  24. }
  25. return $m;
  26. case ‘array’:
  27. foreach($m as $k => $v) {
  28. $m[tb_json_convert_encoding($k, $from, $to)] = tb_json_convert_encoding($v, $from, $to);
  29. }
  30. return $m;
  31. default:
  32. }
  33. return $m;
  34. }
  35. ?>
复制代码