• PHP中json_encode中文编码的问题_学习


     /**
         *  由于php的json扩展自带的函数json_encode会将汉字转换成unicode码
         *  所以我们在这里用自定义的json_encode,这个函数不会将汉字转换为unicode码
         */
        public function customJsonEncode($a = false) {
            if (is_null($a))
                return 'null';
            if ($a === false)
                return 'false';
            if ($a === true)
                return 'true';
            if (is_scalar($a)) { //判断是否为一个标量
                if (is_float($a)) {
                    return floatval(str_replace(",", ".", strval($a))); //将变量转换为字段串类型
                    //floatval() 将变量转换为浮点类型
                }
                if (is_string($a)) {
                    static $jsonReplaces = array(array("\", "/", "
    ", "	", "
    ", "", "f", '"'), array('\\', '\/', '\n', '\t', '\r', '\b', '\f', '"'));
                    return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
                } else {
                    return $a;
                }
            }
    
            $isList = true; //判断键值是否为自增长,也就是键值是从0开始自动添加的,不是自定义的
            for ($i = 0, reset($a); $i < count($a); $i++, next($a)) {
                if (key($a) !== $i) {
                    $isList = false;
                    break;
                }
            }
            $result = array();
            if ($isList) {
                foreach ($a as $v)
                    $result[] = $this->customJsonEncode($v);
                return '[' . join(',', $result) . ']';
            } else {
                foreach ($a as $k => $v)
                    $result[] = $this->customJsonEncode($k) . ':' . $this->customJsonEncode($v);
                return '{' . join(',', $result) . '}';
            }
        }
    

      

  • 相关阅读:
    关于Xcode的一些方法-15-05-01
    iOS 多线程(NSThread、GCD、NSOperation)
    iOS中View的创建过程
    iOS启动原理及应用生命周期
    UITableView详解
    iOS 字典转模型
    strong和weak
    零碎知识点总结(不定时更新)
    iOS常用第三方类库 Xcode插件
    cocoapods 类库管理利器
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/3452251.html
Copyright © 2020-2023  润新知