无法在magento2中获取jsonEncode [英] unable to get jsonEncode in magento2

查看:108
本文介绍了无法在magento2中获取jsonEncode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Magento has its own json encode and decode functions:

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

上面的代码在Magento 2中已贬值.那么如何使用jsonEncode,使用json Encode我必须扩展什么?

Above code in depreciated in Magento 2. So how to use jsonEncode, what I have to extend to use json Encode?

推荐答案

Magento 2方法是使用DI功能传递Magento\Framework\Json\Helper\Data(请参见打击).不要使用$this->helper()objectManager.此功能即将弃用.

Magento 2 way is pass Magento\Framework\Json\Helper\Data using DI functionality (see blow). Don't use $this->helper() and objectManager. This functionality will be deprecated soon.

/**
 * @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;
}

这篇关于无法在magento2中获取jsonEncode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆