【问题标题】:unable to get jsonEncode in magento2无法在 magento2 中获取 jsonEncode
【发布时间】:2016-08-31 06:39:21
【问题描述】:

Magento 有自己的 json 编码和解码功能:

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

上面的代码在 Magento 2 中被贬值了。那么如何使用 jsonEncode,我必须扩展什么才能使用 json Encode?​​p>

【问题讨论】:

    标签: json magento magento2


    【解决方案1】:

    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;
    }
    

    【讨论】:

    • 我参考了 branko ajzele 的 magento 2 开发人员指南,第 171 页,其中明确给出了使用对象管理器。我认为 objcetmanager 是最好的
    • 我假设本指南不正确。请查看 Magento 架构 Anton Kril 的答案:magento.stackexchange.com/questions/10517/…
    • 已经不推荐使用该助手。请改用Magento\Framework\Serialize\Serializer\Json - 有serializeunserialize 方法。
    • \Magento\Framework\Json\Helper\Data 已弃用 @ 101.0.0,Magento 将在将来使用 \Magento\Framework\Serialize\Serializer\Json 时删除此类 \Magento\Framework\Serialize\Serializer\Json
    【解决方案2】:

    使用 \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

    【讨论】:

      【解决方案3】:

      试试:

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

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

      【讨论】:

        【解决方案4】:

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

        \Magento\Framework\Serialize\JsonConverter::convert($data)
        

        【讨论】:

          【解决方案5】:

          你可以使用下面的代码

          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;
          }
          

          【讨论】:

            【解决方案6】:
             use Magento\Framework\Json\Helper\Data;        
            
             protected $jsonHelper;
            
             public function __construct(
                      Data $jsonHelper
                    ) {
            
                     $this->jsonHelper = $jsonHelper;
            
                    }
            
             protected function jsonResponse()
                {
                 $this->jsonHelper->jsonEncode($array);
                }
            

            【讨论】:

              【解决方案7】:

              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
              

              【讨论】:

                【解决方案8】:

                如果您只想将给定数组编码为 JSON,则应使用直接 PHP 函数,即json_encode

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

                【讨论】:

                  猜你喜欢
                  • 2019-12-24
                  • 1970-01-01
                  • 2021-08-29
                  • 2016-03-24
                  • 2016-04-25
                  • 2016-12-07
                  • 1970-01-01
                  • 2016-08-15
                  • 1970-01-01
                  相关资源
                  最近更新 更多