php5.2以下版本无json_decode函数的解决方法

yipeiwu_com6年前PHP代码库
今天写代码的时候,需要用到json_decode函数,发现php5.2以前的版本没有集成这个函数,不过我们可以通过自定义函数实现。

复制代码 代码如下:

function json_decode2($json)
{
$comment = false;
$out = '$x=';

for ($i=0; $i<strlen($json); $i++)
{
if (!$comment)
{
if (($json[$i] == '{') || ($json[$i] == '[')) $out .= ' array(';
else if (($json[$i] == '}') || ($json[$i] == ']')) $out .= ')';
else if ($json[$i] == ':') $out .= '=>';
else $out .= $json[$i];
}
else $out .= $json[$i];

if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
}

    eval($out . ';');
return $x;
}

不过这个返回的是Array

要返回object 则要用到 service_json类了

相关文章

php数组函数序列之array_key_exists() - 查找数组键名是否存在

array_key_exists() 定义和用法 array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 fal...

php匹配字符中链接地址的方法

本文实例讲述了php匹配字符中链接地址的方法。分享给大家供大家参考。具体如下: 判断一个字符串是否含有超级链接,代码如下: 复制代码 代码如下:$str="ssdsf<a targ...

PHP运行时强制显示出错信息的代码

复制代码 代码如下: error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('error_log', dirna...

php过滤输入操作之htmlentities与htmlspecialchars用法分析

本文实例讲述了php过滤输入操作htmlentities与htmlspecialchars用法。分享给大家供大家参考,具体如下: 过滤输入 (即来自所列数据源中的任何数据)是指,转义或删...

谈谈从phpinfo中能获取哪些值得注意的信息

谈谈从phpinfo中能获取哪些值得注意的信息

phpinfo函数 phpinfo函数 PHP中提供了PHPInfo()函数,该函数返回 PHP 的所有信息,包括了 PHP 的编译选项及扩充配置、PHP 版本、服务器信息及环境变量、P...