How do I encode an array to JSON without json_encode()?
I'm very very new to PHP, but now I have to help my friend to solve a PHP problem.
He has bought a web site based on PHP, after he upload it to the host, found there is a error:
Fatal error: Call to undefined function json_encode() in xxx.php
The code is very simple:
echo json_encode(array(
'result' => true,
));
It send a json to client.
I know json_encode
is added after php 5.2, but the PHP version of his host is PHP 5.1.2, so that's the reason why it reports error.
But we don't have the right to upgrade the PHP version of host, so I have to modify the code. How to let it return a json to client without json_encode
?
The webpage in client browser will run this javascript code, to get and check that json:
jQuery.ajax({ 'url': '...', ...,
'success':function(json) {
if(json != null && json.result == true) {
// do something
}
}
}
Thanks a lot!
UPDATE
Since I have no rights to upgrade anything or install new libraries to that host, the only thing I can do is change the code. But I nearly know nothing about php, that I don't know how to use 3rd party libraries either. At last, I fix it as this:
In php:
echo '{"result":"true"}';
In javascript:
if(json!=null && json.result=="tr开发者_运维技巧ue") {
// do something
}
First of all: you should strongly consider upgrading, since PHP 5.1.x and 5.2.x are no longer supported. You indicated that you do not have the rights to do so, but if you are a paying customer, that should count for something. PHP 5.2 is deprecated as of the last PHP 5.3.6 release.
A user on PHP.net has provided for a custom function that does the same.
You could rename it to json_decode()
and wrap it in a if (!function_exists('json_encode'))
statement:
if (!function_exists('json_encode')) {
function json_encode() {
// do stuff
}
}
He has an older PHP version where json_encode is missing. This can be fixed with various 3rd party libraries.
- A dated project of mine: http://include-once.org/p/upgradephp/ which provides a drop-in
json_encode
/json_decode
almost identical to PHPs builtin extension (quirky in regards to Unicode). - Then there is https://pear.php.net/package/Services_JSON which even adds JSOL (Object Literals are a superset of JSON) support.
- And also http://framework.zend.com/manual/en/zend.json.html which provides a class delivering a pretty thorough reimplementation. (Probably the best-tested version available.)
You can use a third party JSON library, such as the one from the PEAR project (they provide all sorts of useful PHP libraries, of varying quality):
http://pear.php.net/pepr/pepr-proposal-show.php?id=198
I'm sure there are more (I'd try searching Google for PHP JSON libraries).
Why not use a plain PHP json-library which you can include in every setup?
I googled this one here for you.
Read the comments for json_encode at http://www.php.net/manual/en/function.json-encode.php where you also find a comment containing a manual implementation for php < 5.2: http://www.php.net/manual/de/function.json-encode.php#100835
Further there are also other implementations available via google.
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.
i had to install JSON Libs the other day on my server, try the following:
Execute the following commands:
pecl install json
touch /etc/php.d/json.ini
echo "extension=json.so" > /etc/php.d/json.ini
service httpd restart
The first will install the json libraries to your machine, the second line will create the json.ini fiel in the php.d directory and the last line will add the line extension=json.so
to the json.ini
file.
Hope this helps!
精彩评论