免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1294 | 回复: 0
打印 上一主题 下一主题

JSON编解码函数的php实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 01:58 |只看该作者 |倒序浏览
由于开发一个ajax file manager for web开源项目,数据交换使用的json格式,后来发现在低版本的php上运行会有问题,仔细调试发现json_decode和json_encode无 法正常工作,于是查阅资料,发现低版本的php没有实现这两个函数,为了兼容性,我只好自己实现一个php版的json编码解码代码,并保证和 json2.js的一致,测试调试并通过,现在将其公布出来,供有相同需求的同学使用:
  1. <?php
  2. /* * ****************************************************************************
  3.  * $base: $
  4.  *
  5.  * $Author: $
  6.  *         Berlin Qin
  7.  *
  8.  * $History: base.js $
  9.  * Berlin Qin 2011/5/15 created
  10.  *
  11.  * $contacted
  12.  * webfmt@gmail.com
  13.  * http://www.webfmt.com
  14.  *
  15.  * *************************************************************************** */
  16. /* ===========================================================================
  17.  * license
  18.  *
  19.  * You may not use this file except in compliance with the License.
     * Software distributed under the License is distributed on an "AS IS" basis,
     *   WITHOUT WARRANTY OF ANY KIND, either express or implied,except that you announce  what to you write.

     * The Initial Developer of the Original Code is Berlin Qin.
    Copyright (C) 2000-2050, Berlin Qin., All Rights Reserved.
  20.  *
  21.  *************************************************************************/

  22. function jsonDecode($json)
  23. {
  24.     $result = array();
  25.     try
  26.     {
  27.         if (PHP_VERSION_ID > 50300)
  28.         {
  29.             $result = (array) json_decode($json);
  30.         }
  31.         else
  32.         {
  33.             $json = str_replace(array("\\\\", "\\\""), array("&#92;", "&#34;"), $json);
  34.             $parts = preg_split("@(\"[^\"]*\")|([\[\]\{\},:])|\s@is", $json, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  35.             foreach ($parts as $index => $part)
  36.             {
  37.                 if (strlen($part) == 1)
  38.                 {
  39.                     switch ($part)
  40.                     {
  41.                         case "[":
  42.                         case "{":
  43.                             $parts[$index] = "array(";
  44.                             break;
  45.                         case "]":
  46.                         case "}":
  47.                             $parts[$index] = ")";
  48.                             break;
  49.                         case ":":
  50.                             $parts[$index] = "=>";
  51.                             break;
  52.                         case ",":
  53.                             break;
  54.                         default:
  55.                             break;
  56.                     }
  57.                 }
  58.             }
  59.             $json = str_replace(array("&#92;", "&#34;", "$"), array("\\\\", "\\\"", "\\$"), implode("", $parts));
  60.             $result = eval("return $json;");
  61.         }
  62.     }
  63.     catch (Exception $e)
  64.     {
  65.         $result = array("error" => $e->getCode());
  66.     }
  67.     return $result;
  68. }

  69. function valueTostr($val)
  70. {
  71.     if (is_string($val))
  72.     {
  73.         $val = str_replace('\"', "\\\"", $val);
  74.         $val = str_replace("\\", "\\\\", $val);
  75.         $val = str_replace("/", "\\/", $val);
  76.         $val = str_replace("\t", "\\t", $val);
  77.         $val = str_replace("\n", "\\n", $val);
  78.         $val = str_replace("\r", "\\r", $val);
  79.         $val = str_replace("\b", "\\b", $val);
  80.         $val = str_replace("\f", "\\f", $val);
  81.         return '"' . $val . '"';
  82.     }
  83.     elseif (is_int($val))
  84.         return sprintf('%d', $val);
  85.     elseif (is_float($val))
  86.         return sprintf('%F', $val);
  87.     elseif (is_bool($val))
  88.         return ($val ? 'true' : 'false');
  89.     else
  90.         return 'null';
  91. }

  92. function jsonEncode($arr)
  93. {
  94.     $result = "{}";
  95.     try
  96.     {
  97.         if (PHP_VERSION_ID > 50300)
  98.         {
  99.             $result = json_encode($arr);
  100.         }
  101.         else
  102.         {
  103.             $parts = array();
  104.             $is_list = false;
  105.             if (!is_array($arr))
  106.             {
  107.                 $arr = (array) $arr;
  108.             }
  109.             $end = count($arr) - 1;
  110.             if (count($arr) > 0)
  111.             {
  112.                 if (is_numeric(key($arr)))
  113.                 {
  114.                     $result = "[";
  115.                     for ($i = 0; $i < count($arr); $i++)
  116.                     {
  117.                         if (is_array($arr[$i]))
  118.                         {
  119.                             $result = $result . jsonEncode($arr[$i]);
  120.                         }
  121.                         else
  122.                         {
  123.                             $result = $result . valueTostr($arr[$i]);
  124.                         }
  125.                         if ($i != $end)
  126.                         {
  127.                             $result = $result . ",";
  128.                         }
  129.                     }
  130.                     $result = $result . "]";
  131.                 }
  132.                 else
  133.                 {
  134.                     $result = "{";
  135.                     $i = 0;
  136.                     foreach ($arr as $key => $value)
  137.                     {
  138.                         $result = $result . '"' . $key . '":';
  139.                         if (is_array($value))
  140.                         {
  141.                             $result = $result . jsonEncode($value);
  142.                         }
  143.                         else
  144.                         {
  145.                             $result = $result . valueTostr($value);
  146.                         }
  147.                         if ($i != $end)
  148.                         {
  149.                             $result = $result . ",";
  150.                         }
  151.                         $i++;
  152.                     }
  153.                     $result = $result . "}";
  154.                 }
  155.             }
  156.             else
  157.             {
  158.                 $result = "[]";
  159.             }
  160.         }
  161.     }
  162.     catch (Exception $e)
  163.     {

  164.     }
  165.     return $result;
  166. }
  167. ?>

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP