php5.2以下版本无json_decode函数的解决方法
更新时间:2014年05月25日 18:52:44 作者:
这篇文章主要介绍了php5.2以下版本无json_decode函数的解决方法,需要的朋友可以参考下
今天写代码的时候,需要用到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;
}
复制代码 代码如下:
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使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
这篇文章主要介绍了php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率,结合完整实例形式对比分析了php分别使用mysqli和pdo扩展连接mysql数据库的执行时间,需要的朋友可以参考下2019-05-05
PHP实现登录搜狐广告获取广告联盟数据的方法【附demo源码】
这篇文章主要介绍了PHP实现登录搜狐广告获取广告联盟数据的方法,涉及php基于curl的远程数据操作相关技巧,需要的朋友可以参考下2016-10-10


最新评论