您的位置:首页 > 编程语言 > PHP开发

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

2014-05-17 11:16 681 查看
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类了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: