无法在magento2中获取jsonEncode

时间:2016-08-31 06:39:21

标签: json magento magento2

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

var obj = [{name:"Diego",lastName:"Gallegos"}, {name:"Juan",lastName:"Perez"}];

for(var i = 0; i < obj.length; i++){
     /* Pass the html of the template on step 1 to the _.template function 
        from underscore (_). Import it from its cdn */
     var row = _.template( $('#row').html() ); 

     /*  Then to the tbody of the of your table then only append each row    
         passing the object from the array. row is now a function that          
         receives an object and compiles the template to html */
     $('tbody').append(row(obj[i])); 
}

以上代码在Magento中折旧2.那么如何使用jsonEncode,我必须扩展为使用json Encode?

8 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:5)

从Magento 2.2开始,不推荐使用\Magento\Framework\Json\Helper\Data, 对于版本&gt; = 2.2,您可以使用SerializerInterface

来自Magento 2 devdocs: 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 < / p>

答案 2 :(得分:1)

尝试:

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

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

您可以使用以下代码

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

答案 5 :(得分:0)

 use Magento\Framework\Json\Helper\Data;        

 protected $jsonHelper;

 public function __construct(
          Data $jsonHelper
        ) {

         $this->jsonHelper = $jsonHelper;

        }

 protected function jsonResponse()
    {
     $this->jsonHelper->jsonEncode($array);
    }

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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

参考:Image 3