Home  >  Article  >  Backend Development  >  Solution to the problem that json_decode function is not available in versions below php5.2_PHP Tutorial

Solution to the problem that json_decode function is not available in versions below php5.2_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:261039browse

When writing code today, I need to use the json_decode function. I found that versions before php5.2 did not integrate this function, but we can implement it through a custom function.

Copy code The code is as follows:

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

for ($i=0; $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;
}

But this returns an Array

To return object, you need to use the service_json class

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/776459.htmlTechArticleWhen I was writing code today, I needed to use the json_decode function and found that versions before php5.2 did not integrate this function. But we can achieve this through custom functions. Copy the code The code is like...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn