1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of Json
- *
- * @author jgao
- */
- class Json {
- //put your code here
- public static function encode($obj) {
- $json = json_encode($obj);
- if ($GLOBALS['OS'] == "linux") { # 还原中文字符串
- return preg_replace_callback('#\\\u([0-9a-f]{4})#i', function ($matches) {
- return iconv('UCS-2LE', 'UTF-8', pack('H4', $matches[1]));
- }, $json);
- } else if ($GLOBALS['OS'] == "win32") { # 还原中文字符串
- return preg_replace_callback('#\\\u([0-9a-f]{4})#i', function ($matches) {
- return iconv('UCS-2BE', 'UTF-8', pack('H4', $matches[1]));
- }, $json);
- }
- }
- public static function decode($data) {
- // 检测是否为json数据,如果不是则尝试解压缩
- $obj = json_decode($data);
- if (is_null($obj)) {
- $str = gzip_decode($data);
- $obj = json_decode($str);
- }
- return $obj;
- }
- }
|