php5.2以下版本無json_decode函數(shù)的解決方法
更新時間:2014年05月25日 18:52:44 作者:
這篇文章主要介紹了php5.2以下版本無json_decode函數(shù)的解決方法,需要的朋友可以參考下
今天寫代碼的時候,需要用到json_decode函數(shù),發(fā)現(xiàn)php5.2以前的版本沒有集成這個函數(shù),不過我們可以通過自定義函數(shù)實現(xiàn)。
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中json_decode()和json_encode()的使用方法
- 淺析php中json_encode()和json_decode()
- PHP json_decode函數(shù)詳細解析
- PHP中遇到BOM、<feff>編碼導致json_decode函數(shù)無法解析問題
- Javascript生成json的函數(shù)代碼(可以用php的json_decode解碼)
- php json_encode與json_decode詳解及實例
- 關于php中的json_encode()和json_decode()函數(shù)的一些說明
- PHP實現(xiàn)json_decode不轉義中文的方法
- 使用PHP json_decode可能遇到的坑與解決方法
相關文章
php中使用ExcelFileParser處理excel獲得數(shù)據(jù)(可作批量導入到數(shù)據(jù)庫使用)
使用ExcelFileParser處理excel獲得數(shù)據(jù) 可以用作批量導入到數(shù)據(jù)庫使用,需要獲取excel數(shù)據(jù)的朋友可以參考下。2010-08-081億條數(shù)據(jù)如何分表100張到Mysql數(shù)據(jù)庫中(PHP)
這篇文章主要介紹了當數(shù)據(jù)量猛增的時候如何把一億條數(shù)據(jù)分表100張到Mysql數(shù)據(jù)庫中,需要的朋友可以參考下2015-07-07