Home > Backend Development > PHP Tutorial > 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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:29:26
Original
1121 people have browsed it

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...
Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
Backslash present in Json
From 1970-01-01 08:00:00
0
0
0
Get: Transfer JSON data
From 1970-01-01 08:00:00
0
0
0
mysql storage json error
From 1970-01-01 08:00:00
0
0
0
javascript - Problems with displaying json data
From 1970-01-01 08:00:00
0
0
0
Find matching integers in JSON.
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template