无法在 magento2 中获取 jsonEncode

unable to get jsonEncode in magento2

Magento 有自己的 json 编码和解码函数:

Mage::helper('core')->jsonEncode($array);  

以上代码在 Magento 2 中已弃用。那么如何使用 jsonEncode,我必须扩展什么才能使用 jsonEncode?

尝试:

echo $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($array);

$jsonHelper = $this->helper('Magento\Framework\Json\Helper\Data');
echo $jsonHelper->jsonEncode($array);

Magento 2 的方式是通过 Magento\Framework\Json\Helper\Data 使用 DI 功能(见下文)。不要使用 $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 2.2 开始不推荐使用 \Magento\Framework\Json\Helper\Data, 对于 >= 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)
}

最终会调用 class 运行 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

您还可以使用以下静态方法:

\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

在constructor()中注入class 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

参考:https://www.php.net/manual/en/function.json-encode.php