【发布时间】:2016-08-31 06:39:21
【问题描述】:
Magento 有自己的 json 编码和解码功能:
Mage::helper('core')->jsonEncode($array);
上面的代码在 Magento 2 中被贬值了。那么如何使用 jsonEncode,我必须扩展什么才能使用 json Encode?p>
【问题讨论】:
Magento 有自己的 json 编码和解码功能:
Mage::helper('core')->jsonEncode($array);
上面的代码在 Magento 2 中被贬值了。那么如何使用 jsonEncode,我必须扩展什么才能使用 json Encode?p>
【问题讨论】:
Magento 2 方式是使用 DI 功能传递Magento\Framework\Json\Helper\Data(见打击)。不要使用$this->helper() 和objectManager。此功能很快就会被弃用。
/**
* @var \Magento\Framework\Json\Helper\Data
*/
protected $jsonHelper;
/**
* Constructor.
*
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
*/
public function __construct(\Magento\Framework\Json\Helper\Data $jsonHelper)
{
$this->jsonHelper = $jsonHelper;
}
/**
* @param array $dataToEncode
* @return string
*/
public function encodeSomething(array $dataToEncode)
{
$encodedData = $this->jsonHelper->jsonEncode($dataToEncode);
return $encodedData;
}
【讨论】:
Magento\Framework\Serialize\Serializer\Json - 有serialize 和unserialize 方法。
使用 \Magento\Framework\Json\Helper\Data 从 Magento 2.2 起已弃用,
对于 >= 2.2 的版本,您可以使用 SerializerInterface
来自 Magento 2 开发文档: https://devdocs.magento.com/guides/v2.2/extension-dev-guide/framework/serializer.html#json-default
use Magento\Framework\Serialize\SerializerInterface;
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct(SerializerInterface $serializer) {
$this->serializer = $serializer;
}
public function encodeSomething($data) {
return $this->serializer->serialize($data)
}
public function decodeSomething($data) {
return $this->serializer->unserialize($data)
}
这最终会调用一个运行 json_encode() 和 json_decode() https://github.com/magento/magento2/blob/2.2/lib/internal/Magento/Framework/Serialize/Serializer/Json.php的类
因此,如果您在 2.1 或更低版本上运行,则可以使用原生 PHP 函数 json_encode() 和 json_decode() 并获得相同的结果,或者使用已弃用的 \Magento\Framework\Json\Helper\Data
【讨论】:
试试:
echo $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($array);
或
$jsonHelper = $this->helper('Magento\Framework\Json\Helper\Data');
echo $jsonHelper->jsonEncode($array);
【讨论】:
您还可以使用以下静态方法:
\Magento\Framework\Serialize\JsonConverter::convert($data)
【讨论】:
你可以使用下面的代码
public function __construct(\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\Json\Decoder $jsonDecoder,
\Magento\Framework\Json\Encoder $jsonEncoder,
\CP\Coreoverride\Serialize\Serializer\Json $serializer
)
{
$this->resultJsonFactory = $resultJsonFactory;
$this->_jsonDecoder = $jsonDecoder;
$this->_jsonEncoder = $jsonEncoder;
$this->_serializer = $serializer;
parent::__construct($context);
}
public function decodeData($data) {
$dataArray = $this->_jsonDecoder->decode($data);
$result = $this->_serializer->unserialize($dataArray);
return $result;
}
public function encodeData($data) {
$dataArray = $this->_jsonEncoder->encode($data);
$result = $this->_serializer->unserialize($dataArray);
return $result;
}
【讨论】:
use Magento\Framework\Json\Helper\Data;
protected $jsonHelper;
public function __construct(
Data $jsonHelper
) {
$this->jsonHelper = $jsonHelper;
}
protected function jsonResponse()
{
$this->jsonHelper->jsonEncode($array);
}
【讨论】:
Magento 2.4.1
在构造函数中注入类()Magento\Framework\Serialize\Serializer\Json
...
use Magento\Framework\Serialize\Serializer\Json as JsonSerialize;
/**
* @var JsonSerialize
*/
private $jsonSerialize;
/**
* Constructor
* @param JsonSerialize $jsonSerialize
*/
public function __construct(
...
JsonSerialize $jsonSerialize
...
) {
...
$this->jsonSerialize = $jsonSerialize;
...
}
然后你可以使用:
$this->jsonSerialize->serialize($data_here) // json_encode
$this->jsonSerialize->unserialize($data_here) // json_decode
【讨论】:
如果您只想将给定数组编码为 JSON,则应使用直接 PHP 函数,即json_encode
【讨论】: