我最近升级到PHP7.0.4和nginx1.8.1,我的应用程序使用Zend (Magento1.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或我的应用程序/服务器有关吗?
谢谢
发布于 2016-04-04 11:08:13
这是json_decode
绝对正常的行为。
如果给定的字符串不是有效的JSON字符串,它将引发此异常。
正如您已经提到的,空字符串也不是有效的JSON字符串。
json_decode('Hello') // invalid
json_decode("Hello") //invalid
但是:
json_decode("'Hello'") // valid JSON string
空字符串将引发自PHP7以来的异常!
“使用第一个参数调用json_decode等于空PHP或值,该值在转换为string后为空字符串(NULL,FALSE),将导致JSON语法错误。”
所以..。要回答您的问题:在我看来,您的应用程序有您需要解决的问题,如果您的PHP版本不是选项之一。
在将JSON字符串传递给json_decode
函数之前,洋芋函数需要检查JSON字符串的有效表示形式。
如果变量不是字符串( {}
)或它是空的,请尝试将其设置为is_string
。
如果这不是问题所在,则可能您的字符串没有按应该编码的方式进行编码。传递给json_decode
的字符串的多次编码也会导致该异常。
发布于 2016-10-05 12:32:17
一个可能的(脏)修复是将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);
就会被使用,它看起来工作得很好。
发布于 2017-10-25 15:50:20
肮脏的黑客:
在我的例子中,在Magento2.2中,PHP7出现了一个问题,其中一个空对象会抛出一个错误。这将阻止产品索引器的完成。
因此,我为本例添加了一个空数组返回(在文件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) {
....
https://stackoverflow.com/questions/36362856
复制相似问题