json_decode function is not part of PHP 5.1, so I cannot use it. Is there any other function for this ver开发者_开发知识库sion?
Before PHP 5.2, you can use the JSON PECL extension.
In fact, it is the extension that has been integrated into PHP 5.2 (quoting) :
As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.
Some other solutions would be to use some component developped in PHP.
Some time ago (about one year ago), I used the Zend_Json
component of Zend Framework, with PHP 5.1.
Even if Zend Framework requires PHP 5.2, that component can be extracted (I think it depends only on one other component -- maybe Zend_Exception
, or something like that) -- and one year ago, it was possible to use it with PHP 5.1.
The official JSON website also links to several PHP-based or PHP-oriented components -- you might want to take a look at that list.
I ran into the same issue running PHP 5.1.6, but I couldn't upgrade or install extensions on my client's server. To make matters worse, the JSON.org site was down when I needed a solution but fortunately this file on Google Code worked perfectly! I would have preferred to actually define json_encode/json_decode, but calling fromJSON() worked just fine.
http://code.google.com/p/simplejson-php/
You're seeing this error because you have a php version earlier than 5.2.0. These functions are included by default in php 5.2.0 and later.
PHP Fatal error: Call to undefined function json_encode()
You can install the PECL extension by running:
pecl install json
It will compile, then add this to your php.ini
file: (mine is in /etc/php5/apache2
)
extension=json.so
Then restart apache.
In my server i can't install JSON PECL extension, because it causes a problem with zend_json that is used in another app. So i found this script that works perfectly.
jsonwrapper: json_encode for earlier versions of PHP 5.x
PHP 5.2 adds the json_encode
function, which turns almost any PHP data structure into valid JavaScript code. Hashes, arrays, arrays of hashes, whatever.
Unfortunately a lot of Linux distributions are still shipping with PHP 5.1.x.
jsonwrapper implements the json_encode
function if it is missing, and leaves it alone if it is already present. So it is nicely future-compatible.
Just add:
require 'jsonwrapper.php';
http://www.boutell.com/scripts/jsonwrapper.html
The Zend framework has Zend_Json. At least it used to a couple of years ago.
http://framework.zend.com/download
You can just pull out the JSON library and use it in a standalone manner.
code
<?php
if ( !function_exists('json_decode') ){
function json_decode($json)
{
// Author: walidator.info 2009
$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] == '"') $comment = !$comment;
}
eval($out . ';');
return $x;
}
}
?>
warning
this is untested, I found it on the internet
link
http://www.php.net/manual/en/function.json-decode.php#91216
I ran into problems with the Services_Json extension on PHP 5.1.3, so I found the following library:
https://github.com/alexmuz/php-json
It is under LGPL, and after a very quick look does not seem to eval input.
You can use jsonwrapper library...
jsonwrapper implements the json_encode function if it is missing, and leaves it alone if it is already present. So it is nicely future-compatible.
Download here: jsonwrapper
To use just do:
require ("jsonwrapper.php");
$data = array('idx1' => 'foo', 'idx2' => 'bar');
echo json_encode($data);
echo json_decode($data);
精彩评论