如何在PHP 5.1中解码json ?

时间:2022-10-30 23:23:30

json_decode function is not part of PHP 5.1, so I cannot use it. Is there any other function for this version?

json_decode函数不是PHP 5.1的一部分,所以我不能使用它。这个版本还有其他功能吗?

8 个解决方案

#1


8  

Before PHP 5.2, you can use the JSON PECL extension.

在PHP 5.2之前,您可以使用JSON PECL扩展。

In fact, it is the extension that has been integrated into PHP 5.2 (quoting) :

实际上,它是集成到PHP 5.2中的扩展(引用):

As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.

在PHP 5.2.0中,默认情况下将JSON扩展打包并编译成PHP。


Some other solutions would be to use some component developped in PHP.

一些其他的解决方案是使用PHP开发的某个组件。

Some time ago (about one year ago), I used the Zend_Json component of Zend Framework, with PHP 5.1.

前一段时间(大约一年前),我使用Zend Framework的Zend_Json组件,使用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.

即使Zend Framework需要PHP 5.2,也可以提取该组件(我认为它只依赖于另一个组件——可能是Zend_Exception,或者类似的东西)——一年前,可以使用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.

官方的JSON网站也链接到几个基于php的或面向php的组件——您可能想看看这个列表。

#2


6  

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.

我在运行PHP 5.1.6时遇到了同样的问题,但我无法在客户机的服务器上升级或安装扩展。更糟糕的是,当我需要一个解决方案时,JSON.org网站就宕机了,但幸运的是,这个文件在谷歌代码上运行得很好!我更倾向于实际定义json_encode/json_decode,但是从json()调用就可以了。

http://code.google.com/p/simplejson-php/

http://code.google.com/p/simplejson-php/

#3


3  

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版本比5.2.0早。这些函数在php 5.2.0和稍后的版本中默认包含。

PHP Fatal error:  Call to undefined function json_encode()

You can install the PECL extension by running:

您可以通过运行来安装PECL扩展:

pecl install json

It will compile, then add this to your php.ini file: (mine is in /etc/php5/apache2)

它将编译,然后将其添加到php中。ini文件:(我的是/etc/php5/apache2)

extension=json.so

Then restart apache.

然后重新启动apache。

#4


1  

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.

在我的服务器中,我不能安装JSON PECL扩展,因为它会导致另一个应用程序中使用的zend_json问题,所以我发现这个脚本非常好用。

jsonwrapper: json_encode for earlier versions of PHP 5.x

jsonwrapper: json_encode用于早期版本的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.

PHP 5.2添加了json_encode函数,它将几乎任何PHP数据结构转换为有效的JavaScript代码。哈希,数组,哈希数组,等等。

Unfortunately a lot of Linux distributions are still shipping with PHP 5.1.x.

不幸的是,许多Linux发行版仍然以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.

jsonwrapper实现了json_encode函数,如果它丢失了,并且如果它已经存在,就将它单独保留。所以它与未来兼容。

Just add:

添加:

require 'jsonwrapper.php';

http://www.boutell.com/scripts/jsonwrapper.html

http://www.boutell.com/scripts/jsonwrapper.html

#5


0  

The Zend framework has Zend_Json. At least it used to a couple of years ago.

Zend框架具有Zend_Json。至少在几年前是这样的。

http://framework.zend.com/download

http://framework.zend.com/download

You can just pull out the JSON library and use it in a standalone manner.

您可以直接取出JSON库并以独立的方式使用它。

#6


0  

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

http://www.php.net/manual/en/function.json-decode.php # 91216

#7


0  

I ran into problems with the Services_Json extension on PHP 5.1.3, so I found the following library:

在PHP 5.1.3中,我遇到了Services_Json扩展的问题,所以我找到了以下的库:

https://github.com/alexmuz/php-json

https://github.com/alexmuz/php-json

It is under LGPL, and after a very quick look does not seem to eval input.

它是在LGPL下的,而且在快速浏览之后,看起来不像eval的输入。

#8


0  

You can use jsonwrapper library...

您可以使用jsonwrapper库…

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.

jsonwrapper实现了json_encode函数,如果它丢失了,并且如果它已经存在,就将它单独保留。所以它与未来兼容。

Download here: jsonwrapper

在这里下载:jsonwrapper

To use just do:

使用:

require ("jsonwrapper.php");


$data = array('idx1' => 'foo', 'idx2' => 'bar');

echo json_encode($data);

echo json_decode($data);

#1


8  

Before PHP 5.2, you can use the JSON PECL extension.

在PHP 5.2之前,您可以使用JSON PECL扩展。

In fact, it is the extension that has been integrated into PHP 5.2 (quoting) :

实际上,它是集成到PHP 5.2中的扩展(引用):

As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.

在PHP 5.2.0中,默认情况下将JSON扩展打包并编译成PHP。


Some other solutions would be to use some component developped in PHP.

一些其他的解决方案是使用PHP开发的某个组件。

Some time ago (about one year ago), I used the Zend_Json component of Zend Framework, with PHP 5.1.

前一段时间(大约一年前),我使用Zend Framework的Zend_Json组件,使用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.

即使Zend Framework需要PHP 5.2,也可以提取该组件(我认为它只依赖于另一个组件——可能是Zend_Exception,或者类似的东西)——一年前,可以使用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.

官方的JSON网站也链接到几个基于php的或面向php的组件——您可能想看看这个列表。

#2


6  

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.

我在运行PHP 5.1.6时遇到了同样的问题,但我无法在客户机的服务器上升级或安装扩展。更糟糕的是,当我需要一个解决方案时,JSON.org网站就宕机了,但幸运的是,这个文件在谷歌代码上运行得很好!我更倾向于实际定义json_encode/json_decode,但是从json()调用就可以了。

http://code.google.com/p/simplejson-php/

http://code.google.com/p/simplejson-php/

#3


3  

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版本比5.2.0早。这些函数在php 5.2.0和稍后的版本中默认包含。

PHP Fatal error:  Call to undefined function json_encode()

You can install the PECL extension by running:

您可以通过运行来安装PECL扩展:

pecl install json

It will compile, then add this to your php.ini file: (mine is in /etc/php5/apache2)

它将编译,然后将其添加到php中。ini文件:(我的是/etc/php5/apache2)

extension=json.so

Then restart apache.

然后重新启动apache。

#4


1  

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.

在我的服务器中,我不能安装JSON PECL扩展,因为它会导致另一个应用程序中使用的zend_json问题,所以我发现这个脚本非常好用。

jsonwrapper: json_encode for earlier versions of PHP 5.x

jsonwrapper: json_encode用于早期版本的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.

PHP 5.2添加了json_encode函数,它将几乎任何PHP数据结构转换为有效的JavaScript代码。哈希,数组,哈希数组,等等。

Unfortunately a lot of Linux distributions are still shipping with PHP 5.1.x.

不幸的是,许多Linux发行版仍然以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.

jsonwrapper实现了json_encode函数,如果它丢失了,并且如果它已经存在,就将它单独保留。所以它与未来兼容。

Just add:

添加:

require 'jsonwrapper.php';

http://www.boutell.com/scripts/jsonwrapper.html

http://www.boutell.com/scripts/jsonwrapper.html

#5


0  

The Zend framework has Zend_Json. At least it used to a couple of years ago.

Zend框架具有Zend_Json。至少在几年前是这样的。

http://framework.zend.com/download

http://framework.zend.com/download

You can just pull out the JSON library and use it in a standalone manner.

您可以直接取出JSON库并以独立的方式使用它。

#6


0  

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

http://www.php.net/manual/en/function.json-decode.php # 91216

#7


0  

I ran into problems with the Services_Json extension on PHP 5.1.3, so I found the following library:

在PHP 5.1.3中,我遇到了Services_Json扩展的问题,所以我找到了以下的库:

https://github.com/alexmuz/php-json

https://github.com/alexmuz/php-json

It is under LGPL, and after a very quick look does not seem to eval input.

它是在LGPL下的,而且在快速浏览之后,看起来不像eval的输入。

#8


0  

You can use jsonwrapper library...

您可以使用jsonwrapper库…

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.

jsonwrapper实现了json_encode函数,如果它丢失了,并且如果它已经存在,就将它单独保留。所以它与未来兼容。

Download here: jsonwrapper

在这里下载:jsonwrapper

To use just do:

使用:

require ("jsonwrapper.php");


$data = array('idx1' => 'foo', 'idx2' => 'bar');

echo json_encode($data);

echo json_decode($data);