How I can change the json format from
[
[
{
"success": [
{
"code": 200,
"response": {
"collection": [
{
"id": "1",
"title": "camp1",
"description": "vishal desc",
"start_date": "2019-02-28 00:00:00",
"end_date": "2001-02-22 00:00:00",
"status": "0",
"desktop_image": "images\\yourope.jpg",
"mobile_image": "images\\yourope.jpg",
"created_at": "2019-02-28 06:50:25",
"updated_at": "2019-03-11 10:16:48",
"other": null
}
]
}
}
]
}
]
]
To this
{
"data": [
{
"success": [
{
"code": 200,
"response": {
"collection": [
{
"id": "1",
"title": "camp1",
"description": "vishal desc",
"start_date": "2019-02-28 00:00:00",
"end_date": "2001-02-22 00:00:00",
"status": "0",
"desktop_image": "images\\yourope.jpg",
"mobile_image": "images\\yourope.jpg",
"created_at": "2019-02-28 06:50:25",
"updated_at": "2019-03-11 10:16:48",
"other": null
}
]
}
}
]
}
]
}
Abhishek Tripathi
2,9152 gold badges21 silver badges38 bronze badges
-
assign your result to a multidimensional array with key 'data'purna gattu– purna gattu2019年03月26日 12:25:42 +00:00Commented Mar 26, 2019 at 12:25
-
Thanks for reply. I try out this but is not working if i am not wrong may be i need to put this "data" while final JSON has been return by system as we doing in D:\wamp64\www\artoreal\app\code\core\Mage\Api2\Model\Renderer\json.php file public function render($data) { /* Added By Nihilent For Customize JSON response*/ //return Zend_Json::encode($data); if($data['messages']){ return json_encode(array('data'=>array_values($data))); }else{ return Zend_Json::encode($data); } }Vishal Bansal– Vishal Bansal2019年03月28日 09:39:39 +00:00Commented Mar 28, 2019 at 9:39
-
Hi vishal, can you post the method how you rendering , so i can check itpurna gattu– purna gattu2019年03月29日 05:44:04 +00:00Commented Mar 29, 2019 at 5:44
-
public function getinfo($id) { $post = $this->_advertisementFactory->create(); $collection = $post->getCollection(); foreach($collection as $item){ if($item['id']==$id){ $data[]=$item->getData(); } } $response['data'][]=array('success'=>array(array('code'=>200,'response'=>array('collection'=>$data)))); return $response; } }Vishal Bansal– Vishal Bansal2019年03月29日 06:00:25 +00:00Commented Mar 29, 2019 at 6:00
1 Answer 1
Finally I got the answer:
Overwrite this file:
\Magento\Framework\Webapi\Rest\Response\Renderer\Json.php
We are overwrite this file : di.xml
preference for="Magento\Framework\Webapi\Rest\Response\Renderer\Json" type="Evolve\Apirenderer\Webapi\Rest\Response\Renderer\Json"
Replace this function by this Orignal:
public function render($data) { return $this->encoder->encode($data); }Replace By This
public function render($data) { return json_encode(array('data'=>array_values($data))); }You will get above response.
fmsthird
4,6224 gold badges18 silver badges42 bronze badges
-
please format the code, hard to understandJafar Pinjar– Jafar Pinjar2019年05月23日 11:08:10 +00:00Commented May 23, 2019 at 11:08
default