zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Codeforces Round #340 (Div. 2)
    搞清楚一下格式化输出
    TYVJ P1091 等差数列 Label:dp
    TYVJ P1098 任务安排 Label:倒推dp 不懂
    TYVJ P1034 尼克的任务 Label:倒推dp
    TYVJ P1045 &&洛谷 1388 最大的算式 Label:dp
    TYVJ P1046 Blast Label:dp
    TYVJ P1051 选课 Label:多叉转二叉&&树形dp(虐心♥)
    TYVJ P1073 加分二叉树 Label:区间dp
    二叉树遍历入门 Lebal:research
  • 原文地址:https://www.cnblogs.com/songzhenghe/p/4582310.html
Copyright © 2011-2022 走看看