0
Magento的1.x中都有自己的JSON解码功能:无法在Magento获得jsonDecode 2
Mage::helper('core')->jsonDecode($array);
因此,如何使用JSON解码在Magento 2.
Magento的1.x中都有自己的JSON解码功能:无法在Magento获得jsonDecode 2
Mage::helper('core')->jsonDecode($array);
因此,如何使用JSON解码在Magento 2.
尝试下面的代码与Magento的2 JSON解码
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$jsonManager = $objectManager->get('\Magento\Framework\Json\Decoder');
return $jsonManager->decode($data);
会见HOD 1:
echo $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonDecode($array);
或者
$jsonHelper = $this->helper('Magento\Framework\Json\Helper\Data');
echo $jsonHelper->jsonDecode($array);
方法2:
/**
* Constructor.
*
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
*/
public function __construct(\Magento\Framework\Json\Helper\Data $jsonHelper)
{
$this->jsonHelper = $jsonHelper;
}
/**
* @param array $dataToDecode
* @return string
*/
public function decodeSomething(array $dataToDecode)
{
$decodedData= $this->jsonHelper->jsonDecode($dataToDecode);
return $decodedData;
}