如何在不使用 json_encode() 的情况下将数组编码为 JSON?
我对 PHP 非常陌生,但现在我必须帮助我的朋友解决 PHP 问题。
他购买了一个基于PHP的网站,将其上传到主机后,发现出现错误:
Fatal error: Call to undefined function json_encode() in xxx.php
代码很简单:
echo json_encode(array(
'result' => true,
));
发送一个json到客户端。
我知道json_encode
是在php 5.2之后添加的,但是他的主机的PHP版本是PHP 5.1.2,所以这就是它报告错误的原因。
但是我们没有权限升级host的PHP版本,所以我必须修改代码。如何让它在没有 json_encode
的情况下返回 json 给客户端?
客户端浏览器中的网页将运行此 javascript 代码,以获取并检查该 json:
jQuery.ajax({ 'url': '...', ...,
'success':function(json) {
if(json != null && json.result == true) {
// do something
}
}
}
非常感谢!
更新
由于我无权升级任何内容或在该主机上安装新库,我唯一能做的就是更改代码。但我对 php 几乎一无所知,也不知道如何使用 3rd 方库。最后,我将其修复为:
在 php 中:
echo '{"result":"true"}';
在 javascript 中:
if(json!=null && json.result=="true") {
// do something
}
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=="true") {
// do something
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先:您应该强烈考虑升级,因为不再支持 PHP 5.1.x 和 5.2.x。您表示您无权这样做,但如果您是付费客户,那应该有意义。自 最后一个 PHP 5.3.6 版本。
PHP.net 上的用户提供了自定义函数 其作用相同。
您可以将其重命名为
json_decode()
并将其包装在if (!function_exists('json_encode'))
语句中: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 aif (!function_exists('json_encode'))
statement:他有一个较旧的 PHP 版本,其中缺少 json_encode。这可以通过各种第三方库来修复。
json_encode
/json_decode
插件(对于 Unicode 来说很奇怪)。He has an older PHP version where json_encode is missing. This can be fixed with various 3rd party libraries.
json_encode
/json_decode
almost identical to PHPs builtin extension (quirky in regards to Unicode).您可以使用第三方 JSON 库,例如 PEAR 项目中的库(它们提供各种有用的 PHP 库,但质量各不相同):
http://pear.php.net/pepr/pepr-proposal-show.php?id=198
我确定有更多(我会尝试在 Google 中搜索 PHP JSON 库)。
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).
为什么不使用一个可以包含在每个设置中的普通 PHP json 库?
我在 google 上为您搜索了这个。
Why not use a plain PHP json-library which you can include in every setup?
I googled this one here for you.
阅读 json_encode 的注释 http://www.php.net/manual /en/function.json-encode.php 您还可以在其中找到包含 php < 的手动实现的注释。 5.2:
http://www.php.net/manual/de/function .json-encode.php#100835
此外,还可以通过谷歌获得其他实现。
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 如果缺少 json_encode 函数,则实现该函数;如果已存在,则将其保留。所以它很好地兼容未来。
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.
前几天我必须在我的服务器上安装 JSON Libs,请尝试以下操作:
执行以下命令:
pecl install json
touch /etc/php.d/json.ini
echo "extension=json.so" > /etc/php.d/json.ini
service httpd restart
第一行将把 json 库安装到你的机器上,第二行将在 php.d 中创建 json.ini 文件目录,最后一行会将
extension=json.so
行添加到json.ini
文件中。希望这有帮助!
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 thejson.ini
file.Hope this helps!