• json_encode替代函数


    <?php
     
    function jsonEncode($var) {
        if (function_exists('json_encode')) {
            return json_encode($var);
        else {
            switch (gettype($var)) {
                case 'boolean':
                    return $var 'true' 'false'// Lowercase necessary!
                case 'integer':
                case 'double':
                    return $var;
                case 'resource':
                case 'string':
                    return '"'str_replace(array(" ", " ", "<", ">", "&"),
                        array(' '' ''x3c''x3e''x26'),
                        addslashes($var)) .'"';
                case 'array':
                    // Arrays in JSON can't be associative. If the array is empty or if it
                    // has sequential whole number keys starting with 0, it's not associative
                    // so we can go ahead and convert it as an array.
                    if (empty ($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
                        $output array();
                        foreach ($var as $v) {
                            $output[] = jsonEncode($v);
                        }
                        return '[ '. implode(', '$output) .' ]';
                    }
                    // Otherwise, fall through to convert the array as an object.
                case 'object':
                    $output array();
                    foreach ($var as $k => $v) {
                        $output[] = jsonEncode(strval($k)) .': '. jsonEncode($v);
                    }
                    return '{ '. implode(', '$output) .' }';
                default:
                    return 'null';
            }
        }
     
    }
     
    $arr=array('name'=>'CSDN论坛','degree'=>'numberone');
     
    echo jsonEncode($arr);
     
    ?>

    转载地址:http://blog.csdn.net/ktz666/article/details/7593040

  • 相关阅读:
    android 启动报错
    android 百度地图
    android LayoutInflater使用
    spring mvc No mapping found for HTTP request with URI [/web/test.do] in DispatcherServlet with name 'spring'
    sql mysql和sqlserver存在就更新,不存在就插入的写法(转)
    jsp include
    json 解析
    css
    Scrapy组件之item
    Scrapy库安装和项目创建
  • 原文地址:https://www.cnblogs.com/songzhenghe/p/4582310.html
Copyright © 2020-2023  润新知