0

I am trying to return the collection factory from database , it is giving an array now i convert it to json using JSON Encode function but it is returnig a string with backslashes !!

Code for Api Interface

<?php
namespace FME\MobileApp\Api;
interface SliderInterface
{
    /**
 * @return Object
 */
public function getImages();

}

Code for API

<?php
namespace FME\MobileApp\Model\Api;
use \FME\MobileApp\Model\ImgFactory;
class SliderImages {
    protected $modelFactory;
    public $_storeManager;

    public function __construct(
    ImgFactory $modelFactory ,
    \Magento\Store\Model\StoreManagerInterface $storeManager 
)
 {
    $this->modelFactory = $modelFactory;
    $this->_storeManager=$storeManager;

    }
    
/**
 * @inheritdoc
 */
public function getImages()
{
    $baseurl =  $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

    $photogallery_id = 1;

    $model = $this->modelFactory->create()->getCollection()
               ->addFieldToFilter('photogallery_id',$photogallery_id);
    
    $results = $model->getData();
    

        return json_encode($results);
 }

}

Output

"[{"img_id":"1","img_name":"\/6\/1\/619c5e9c0fe4d_6.jpg","img_label":"","img_url":"http:\/\/localhost\/m2422\/pub\/media\/mobapp\/images\/6\/1\/619c5e9c0fe4d_6.jpg","photogallery_id":"1","img_order":"1","disabled":"0","img_description":"","tags":""}]"

1 Answer 1

0

You need to use code like this one.

use Magento\Framework\Serialize\Serializer\Json;

class SliderImages 
{
    protected $serialize;

    public function __construct(Json $serialize)
    {
        $this->serialize = $serialize;
    }

    public function getImages()
    {
        $results = $model->getData();
        return $this->serialize->serialize($results);
    }

}

Your Answer

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

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