首頁  >  文章  >  後端開發  >  GBK頁面輸出JSON的php程式碼

GBK頁面輸出JSON的php程式碼

WBOY
WBOY原創
2016-07-25 09:02:46974瀏覽
  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. ?>

复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn