No json_for versions below php 5.2Solution of decode function

  • 2021-06-28 11:51:27
  • OfStack

json_is required when writing code todaydecode function, found that pre-php5.2 versions did not integrate this function, but we can implement it through custom functions.


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;
}

But this returns Array

service_is used to return objectjson class


Related articles: