如何在 PHP 5.1 中解码 json?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2370968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I decode json in PHP 5.1?
提问by newbie
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 的一部分,所以我不能使用它。这个版本还有其他功能吗?
采纳答案by Pascal MARTIN
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_Jsoncomponent of Zend Framework, with PHP 5.1.
前段时间(大约一年前),我使用了Zend_JsonZend Framework的组件,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 websitealso links to several PHP-based or PHP-oriented components -- you might want to take a look at that list.
该官方网站JSON也链接到一些基于PHP或PHP的面向组件-你可能想看看该列表。
回答by Marcy Sutton
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 站点已关闭,但幸运的是,Google Code 上的这个文件运行良好!我宁愿实际定义 json_encode/json_decode,但调用 fromJSON() 工作得很好。
回答by jjclarkson
You're seeing this error because you have a php version earlier than 5.2.0. These functions are included by defaultin 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 installthe PECL extensionby running:
pecl install json
It will compile, then add this to your php.inifile: (mine is in /etc/php5/apache2)
它将编译,然后将其添加到您的php.ini文件中:(我的在/etc/php5/apache2)
extension=json.so
Then restart apache.
然后重启apache。
回答by daniel__
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:用于早期版本的 PHP 5.x 的 json_encode
PHP 5.2 adds the json_encodefunction, 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_encodefunction 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';
回答by personne3000
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 下,经过快速查看似乎没有评估输入。
回答by Slipstream
You can use jsonwrapperlibrary...
您可以使用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);
回答by Oli
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 库并以独立方式使用它。
回答by Adam Kiss
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
这是未经测试的,我在互联网上找到的

