PHP7,JSON和Zend(解码失败:语法错误)

时间:2016-04-01 17:57:07

标签: json zend-framework php-7

我最近升级到PHP 7.0.4和nginx 1.8.1,我的应用程序使用Zend Framework(Magento 1.9.2.1)。自从升级以来,我们的客户在提交投入的订单时有时会收到“解码失败:语法错误”

public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
{
    $encodedValue = (string) $encodedValue;
    if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
        $decode = json_decode($encodedValue, $objectDecodeType);

        // php < 5.3
        if (!function_exists('json_last_error')) {
            if (strtolower($encodedValue) === 'null') {
                return null;
            } elseif ($decode === null) {
                #require_once 'Zend/Json/Exception.php';
                throw new Zend_Json_Exception('Decoding failed');
            }
        // php >= 5.3
        } elseif (($jsonLastErr = json_last_error()) != JSON_ERROR_NONE) {
            #require_once 'Zend/Json/Exception.php';
            switch ($jsonLastErr) {
                case JSON_ERROR_DEPTH:
                    throw new Zend_Json_Exception('Decoding failed: Maximum stack depth exceeded');
                case JSON_ERROR_CTRL_CHAR:
                    throw new Zend_Json_Exception('Decoding failed: Unexpected control character found');
                case JSON_ERROR_SYNTAX:
                    throw new Zend_Json_Exception('Decoding failed: Syntax error');
                default:
                    throw new Zend_Json_Exception('Decoding failed');
            }
        }

        return $decode;
    }

我读到了编码空字符串时PHP7和JSON解码行为不同的错误。有谁知道这个bug是否与PHP7或我的应用程序/服务器有关?

由于

5 个答案:

答案 0 :(得分:8)

这是json_decode的绝对正常行为。

如果给定的字符串不是有效的JSON字符串,它将抛出此异常。

如前所述,空字符串也不是有效的JSON字符串。

json_decode('Hello') // invalid
json_decode("Hello") //invalid

可是:

json_decode("'Hello'") // valid JSON string

自PHP7起,空字符串将引发异常!

  

&#34;调用第一个参数的json_decode等于空PHP字符串或者在转换为字符串后为空字符串(NULL,FALSE)导致JSON语法错误。&#34;

所以...回答你的问题:在我看来,你的应用程序有你需要解决的问题,如果不能降级你的PHP版本。

Magenta函数需要在将JSON字符串传递给json_decode函数之前检查其是否有效。

如果变量不是字符串({})或者为空,请尝试将变量设置为is_string

如果这不是问题,则可能是您的字符串未按预期编码。很多时候,传递给json_decode的字符串的编码也会导致该异常。

答案 1 :(得分:0)

一个可能的(脏)修复是将Zend的解码器中的构建设置为true,而这可能会慢得多,这可以在PHP7上运行,其中默认包出错。

在课程Zend_Json

,将$useBuiltinEncoderDecoder设为true;

public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
{
    $encodedValue = (string) $encodedValue;
    if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {

这样会使用return Zend_Json_Decoder::decode($encodedValue, $objectDecodeType);,这似乎工作正常。

答案 2 :(得分:0)

肮脏的黑客:

在我的情况下,使用PHP7的Magento 2.2中存在问题,其中空对象会引发错误。这会阻止产品索引器完成。

所以我为这种情况添加了一个空数组返回(在文件vendor / magento / zendframework1 / library / Zend / Json.php中):

    public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
    {
        $encodedValue = (string) $encodedValue;

        if($encodedValue == "a:0:{}") { return []; } //<- added: return an empty array

        if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
....

答案 3 :(得分:0)

我有同样的问题。我有一个临时修复如下,等待更好的解决方案

  //return Zend_Json::decode($data->params);
    return json_decode($data->params);

答案 4 :(得分:0)

这已在zend-mvc 3.1.1

中修复