我被困在使用 PHP 5.2 之前的版本 (v 5.1.6),因此无法访问像 json_decode() 这样的方便函数
更复杂的是,我也没有服务器权限来安装任何扩展。
简单地包含一个类定义,我可以用来从一串 json 数据创建对象(甚至是复杂的数组),那就太好了。
有谁知道一个轻量级和可靠的类定义对我有用?
(我真的不想在这里重新发明轮子。
提前感谢!
您可以直接下载 pear JSON 库,并将脚本包含在您的应用程序中。 查看此链接: http://pear.php.net/package/Services_JSON/download
你可以在 PEAR 上使用 Services_JSON,你会像这样使用它:
if ( !function_exists('json_decode') ){
function json_decode($content, $assoc=false){
include_once('JSON.php');
if ( $assoc ){
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
} else {
$json = new Services_JSON;
}
return $json->decode($content);
}
function json_encode($content){
include_once('JSON.php');
$json = new Services_JSON;
return $json->encode($content);
}
}
查看Zend_Json,似乎还可以,但我不知道所需的最低 php 版本。