1

I have used below code to print response from my Model file.

<?php

class CountrylistManagement implements 
   \Vendor\Module\Api\CountrylistManagementInterface
{
protected $_storeManager;
protected $objectManager;
protected $_resource;
protected $resultJsonFactory;
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\ObjectManagerInterface $objectManager,       
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
) {
    $this->_objectManager = $objectManager;     
    $this->_storeManager = $storeManager;
    $this->_resource = $resource;
    $this->resultJsonFactory = $resultJsonFactory;      
    parent::__construct($context);
}
     public function postCountrylist($params)
{
    $resultArr = array();
    $request =  json_encode($params);
    $resultArr['message'] = "Success";
    $resultArr['code'] = 200;
    $resultJson = $this->resultJsonFactory->create();
    $resultJson->setData($resultArr);
    return $resultJson;
  }
}

Here I am trying to print the json response from the model file, The above code works well from controller.

Can we use $resultJson = $this->resultJsonFactory->create(); to print the response from model file?

right now its not working for me. if anyone can look into this and update where i am doing wrong please.

1 Answer 1

1

Please donot use \Magento\Framework\Controller\Result\JsonFactory

Use Magento\Framework\Serialize\Serializer\Json for make json response.

You have to make use serialize($resultArr) for getting json response.

Code:

<?php

namespace Devamitbera\Systemconfig\Model;

class CountrylistManagement implements \Vendor\Module\Api\CountrylistManagementInterface {

    /**
     * @var \Magento\Framework\Serialize\Serializer\Json
     */
    private $json;
    protected $_storeManager;
    protected $objectManager;
    protected $_resource;

    public function __construct(
            \Magento\Framework\ObjectManagerInterface $objectManager,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\App\ResourceConnection $resource,
            \Magento\Framework\Serialize\Serializer\Json $json
    ) {
        $this->_objectManager = $objectManager;
        $this->_storeManager = $storeManager;
        $this->_resource = $resource;
        $this->json = $json;
    }

    public function postCountrylist($params) {
        $resultArr = array();
        $resultArr['message'] = "Success";
        $resultArr['code'] = 200;

        return $this->json->serialize($resultArr);
    }

}
11
  • can u update to my question please, May 23, 2019 at 11:19
  • I've used like below now, use Magento\Framework\Serialize\Serializer\Json; then return serialize($resultArr);, but still its not working May 23, 2019 at 11:28
  • I think you need to remove \Magento\Framework\App\Action\Context $context, and parent::__construct($context);
    – Amit Bera
    May 23, 2019 at 11:33
  • now i am able to print in json format, "{\"status code\":\"200\",,\"result\":\"Success\"}" can we print as like this, { "status code": 200, "result": "Success" } May 23, 2019 at 11:42
  • previously i used like this, echo json_encode($resultArr,JSON_PRETTY_PRINT); this was working fine, but i need to use in magento format May 23, 2019 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.