PHP-调用未定义的函数json_encode()

PHP-调用未定义的函数json_encode(),php,json,Php,Json,我知道还有其他关于此错误的主题,但我想问的是,如何确认我的托管服务器上是否有此错误,如果没有,如何添加此错误: phpinfo返回: PHP Version 5.5.14 JSON Omar Kilani, Scott MacVicar 但我的错误日志显示: PHP致命错误:调用中未定义的函数json_encode() 我认为5.5.14应该包含JSON 谢谢只要用PECL安装JSON包就行了,因为不是每个PHP发行版都有JSON包 安装说明只需使用PECL安装JSON包,因为并非每个P

我知道还有其他关于此错误的主题,但我想问的是,如何确认我的托管服务器上是否有此错误,如果没有,如何添加此错误:

phpinfo返回:

PHP Version 5.5.14
JSON    Omar Kilani, Scott MacVicar
但我的错误日志显示:
PHP致命错误:调用中未定义的函数json_encode()

我认为5.5.14应该包含JSON


谢谢

只要用PECL安装JSON包就行了,因为不是每个PHP发行版都有JSON包


安装说明

只需使用PECL安装JSON包,因为并非每个PHP发行版都有JSON包


安装说明

以下是我在PHP
5.1中成功使用的内容

  function json_encode($a=false)
    {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
            if (is_float($a))
            {
                // Always use "." for floats.
                return floatval(str_replace(",", ".", strval($a)));
            }

            if (is_string($a))
            {
                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
            }
            else
            return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
            if (key($a) !== $i)
            {
                $isList = false;
                break;
            }
        }
        $result = array();
        if ($isList)
        {
            foreach ($a as $v) $result[] = json_encode($v);
            return '[' . join(',', $result) . ']';
        }
        else
        {
            foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
            return '{' . join(',', $result) . '}';
        }
    }

  function json_decode($json) 
  {  

    $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; 
} 
函数json_encode($a=false) { 如果(is_null($a))返回“null”; 如果($a==false)返回“false”; if($a==true)返回“true”; 如果(是标量($a)) { 如果(是浮动($a)) { //浮动时始终使用“.”。 返回floatval(str_替换(“,”,“,”,strval($a)); } 如果(是字符串($a)) { 静态$jsonReplaces=array(数组(“\\\”、“/”、“\n”、“\t”、“\r”、“\b”、“\f”、“““”)、数组(“\\\”、“\\\/”、“\\n”、“\\t”、“\\r”、“\\b”、“\\f”、“\”); 返回“'.str_replace($jsonReplaces[0],$jsonReplaces[1],$a)。”; } 其他的 返回$a; } $isList=true; 对于($i=0,重置($a);$i$v)$result[]=json_encode($k)。“:”.json_encode($v); 返回“{”.join(“,”,$result)。“}”; } } 函数json_decode($json) { $comment=false; $out='$x=';
对于($i=0;$i以下是我成功用于PHP
5.1

  function json_encode($a=false)
    {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
            if (is_float($a))
            {
                // Always use "." for floats.
                return floatval(str_replace(",", ".", strval($a)));
            }

            if (is_string($a))
            {
                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
            }
            else
            return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
            if (key($a) !== $i)
            {
                $isList = false;
                break;
            }
        }
        $result = array();
        if ($isList)
        {
            foreach ($a as $v) $result[] = json_encode($v);
            return '[' . join(',', $result) . ']';
        }
        else
        {
            foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
            return '{' . join(',', $result) . '}';
        }
    }

  function json_decode($json) 
  {  

    $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; 
} 
函数json_encode($a=false) { 如果(is_null($a))返回“null”; 如果($a==false)返回“false”; if($a==true)返回“true”; 如果(是标量($a)) { 如果(是浮动($a)) { //浮动时始终使用“.”。 返回floatval(str_替换(“,”,“,”,strval($a)); } 如果(是字符串($a)) { 静态$jsonReplaces=array(数组(“\\\”、“/”、“\n”、“\t”、“\r”、“\b”、“\f”、“““”)、数组(“\\\”、“\\\/”、“\\n”、“\\t”、“\\r”、“\\b”、“\\f”、“\”); 返回“'.str_replace($jsonReplaces[0],$jsonReplaces[1],$a)。”; } 其他的 返回$a; } $isList=true; 对于($i=0,重置($a);$i$v)$result[]=json_encode($k)。“:”.json_encode($v); 返回“{”.join(“,”,$result)。“}”; } } 函数json_decode($json) { $comment=false; $out='$x=';
对于($i=0;$i使用php-m并查看json模块是否正在加载Hanks-这表明它没有被加载..我如何加载它?您使用的是哪个操作系统?首先,您需要安装此模块并将其添加到php.in like extension=json。因此,根据osI运行的openSuSe 13.2,使用php-m并查看json模块是否正在加载Hanks-这表明它没有被加载g loaded..如何加载?您使用的是哪个操作系统?首先,您需要安装此模块并将其添加到php.in like extension=json.so中。因此,根据osI运行的openSuSe 13.2I建议使用内置函数。无法保证自定义解决方案可以处理所有边缘情况,并且不会受到攻击。我建议使用buil不能保证定制解决方案能够处理所有边缘情况,并且不会受到攻击。