读书人

php5.1.6如何解析json

发布时间: 2012-04-10 21:03:56 作者: rapoo

php5.1.6怎么解析json
服务端得到一段json数据 我想把json数据转换为数组 但 5.2以下版本的php不支持 json_decode()函数
求解决办法

[解决办法]
可以自已循环拼接啊!
[解决办法]
帮你找了段,凑合用吧

PHP code
function _json_decode($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;}$json='{"name":"hello world!","arr":[1,2,3,4]}';print_r(_json_decode($json)); 

读书人网 >PHP

热点推荐