如何在没有json_encode()的情况下将数组编码为JSON?

时间:2022-05-10 00:28:12

I'm very very new to PHP, but now I have to help my friend to solve a PHP problem.

我对PHP非常陌生,但现在我必须帮助我的朋友解决PHP问题。

He has bought a web site based on PHP, after he upload it to the host, found there is a error:

他已经购买了一个基于PHP的网站,在他上传到主机后,发现有一个错误:

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.

它将json发送给客户端。

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.

我知道在php 5.2之后添加了json_encode,但是他的主机的PHP版本是PHP 5.1.2,所以这就是它报告错误的原因。

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?

但是我们没有权利升级主机的PHP版本,因此我必须修改代码。如何让它在没有json_encode的情况下将json返回给客户端?

The webpage in client browser will run this javascript code, to get and check that json:

客户端浏览器中的网页将运行此javascript代码,以获取并检查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:

由于我没有权利升级任何东西或安装新的库到该主机,我唯一能做的就是更改代码。但我对php几乎一无所知,我也不知道如何使用第三方库。最后,我修复它:

In php:

echo '{"result":"true"}';

In javascript:

if(json!=null && json.result=="true") {
    // do something
}

8 个解决方案

#1


6  

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.

首先:您应该强烈考虑升级,因为不再支持PHP 5.1.x和5.2.x.您表示您没有权利这样做,但如果您是付费客户,则应该算一些事情。从上一个PHP 5.3.6版本开始,不推荐使用PHP 5.2。

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:

PHP.net上的用户提供了相同的自定义功能。您可以将其重命名为json_decode()并将其包装在if(!function_exists('json_encode'))语句中:

if (!function_exists('json_encode')) {
     function json_encode() {
         // do stuff
     }
}

#2


3  

He has an older PHP version where json_encode is missing. This can be fixed with various 3rd party libraries.

他有一个较旧的PHP版本,缺少json_encode。这可以通过各种第三方库来修复。

  • 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).
  • 我的一个过时的项目:http://include-once.org/p/upgradephp/,它提供了一个与PHP内置扩展几乎完全相同的插件json_encode / json_decode(关于Unicode的古怪)。

  • Then there is https://pear.php.net/package/Services_JSON which even adds JSOL (Object Literals are a superset of JSON) support.
  • 然后是https://pear.php.net/package/Services_JSON,它甚至添加了JSOL(Object Literals是JSON的超集)支持。

  • 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.)
  • 还有http://framework.zend.com/manual/en/zend.json.html,它提供了一个提供非常彻底的重新实现的类。 (可能是经过最佳测试的版本。)

#3


1  

There's an implementation you can use posted in the comments of the PHP docs for json_encode.

您可以在json_encode的PHP文档的注释中发布一个实现。

#4


1  

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):

您可以使用第三方JSON库,例如PEAR项目中的库(它们提供各种有用的PHP库,质量各异):

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).

我相信还有更多(我会尝试在Google上搜索PHP JSON库)。

#5


0  

Why not use a plain PHP json-library which you can include in every setup?

为什么不使用一个普通的PHP json库,你可以在每个设置中包含它?

I googled this one here for you.

我在这里用Google搜索了这个。

#6


0  

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

在http://www.php.net/manual/en/function.json-encode.php上阅读json_encode的注释,您还可以在其中找到包含php <5.2的手动实现的注释:http://www.php。净/手动/德/ function.json-encode.php#100835

Further there are also other implementations available via google.

此外,还有其他可通过谷歌实现的实现。

#7


0  

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_encode函数,jsonwrapper会实现json_encode函数,如果它已经存在则不再使用它。所以很好的未来兼容。

#8


0  

i had to install JSON Libs the other day on my server, try the following:

我不得不在我的服务器上安装JSON Libs,尝试以下方法:

Execute the following commands:

执行以下命令:

  • pecl install json
  • pecl安装json

  • touch /etc/php.d/json.ini
  • echo "extension=json.so" > /etc/php.d/json.ini
  • echo“extension = json.so”> /etc/php.d/json.ini

  • service httpd restart
  • 服务httpd重启

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.

第一行将json库安装到您的机器上,第二行将在php.d目录中创建json.ini文件,最后一行将把行extension = json.so添加到json.ini文件中。

Hope this helps!

希望这可以帮助!

#1


6  

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.

首先:您应该强烈考虑升级,因为不再支持PHP 5.1.x和5.2.x.您表示您没有权利这样做,但如果您是付费客户,则应该算一些事情。从上一个PHP 5.3.6版本开始,不推荐使用PHP 5.2。

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:

PHP.net上的用户提供了相同的自定义功能。您可以将其重命名为json_decode()并将其包装在if(!function_exists('json_encode'))语句中:

if (!function_exists('json_encode')) {
     function json_encode() {
         // do stuff
     }
}

#2


3  

He has an older PHP version where json_encode is missing. This can be fixed with various 3rd party libraries.

他有一个较旧的PHP版本,缺少json_encode。这可以通过各种第三方库来修复。

  • 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).
  • 我的一个过时的项目:http://include-once.org/p/upgradephp/,它提供了一个与PHP内置扩展几乎完全相同的插件json_encode / json_decode(关于Unicode的古怪)。

  • Then there is https://pear.php.net/package/Services_JSON which even adds JSOL (Object Literals are a superset of JSON) support.
  • 然后是https://pear.php.net/package/Services_JSON,它甚至添加了JSOL(Object Literals是JSON的超集)支持。

  • 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.)
  • 还有http://framework.zend.com/manual/en/zend.json.html,它提供了一个提供非常彻底的重新实现的类。 (可能是经过最佳测试的版本。)

#3


1  

There's an implementation you can use posted in the comments of the PHP docs for json_encode.

您可以在json_encode的PHP文档的注释中发布一个实现。

#4


1  

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):

您可以使用第三方JSON库,例如PEAR项目中的库(它们提供各种有用的PHP库,质量各异):

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).

我相信还有更多(我会尝试在Google上搜索PHP JSON库)。

#5


0  

Why not use a plain PHP json-library which you can include in every setup?

为什么不使用一个普通的PHP json库,你可以在每个设置中包含它?

I googled this one here for you.

我在这里用Google搜索了这个。

#6


0  

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

在http://www.php.net/manual/en/function.json-encode.php上阅读json_encode的注释,您还可以在其中找到包含php <5.2的手动实现的注释:http://www.php。净/手动/德/ function.json-encode.php#100835

Further there are also other implementations available via google.

此外,还有其他可通过谷歌实现的实现。

#7


0  

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_encode函数,jsonwrapper会实现json_encode函数,如果它已经存在则不再使用它。所以很好的未来兼容。

#8


0  

i had to install JSON Libs the other day on my server, try the following:

我不得不在我的服务器上安装JSON Libs,尝试以下方法:

Execute the following commands:

执行以下命令:

  • pecl install json
  • pecl安装json

  • touch /etc/php.d/json.ini
  • echo "extension=json.so" > /etc/php.d/json.ini
  • echo“extension = json.so”> /etc/php.d/json.ini

  • service httpd restart
  • 服务httpd重启

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.

第一行将json库安装到您的机器上,第二行将在php.d目录中创建json.ini文件,最后一行将把行extension = json.so添加到json.ini文件中。

Hope this helps!

希望这可以帮助!