CakePHP Json response without key

CakePHP Json response without key



I'm using cakephp to write a webservice. So to response a json result the I used to _serialize, but _serialize require an array with the key => value


return array(
'data' => $data,
'_serialize' => array('data'));



And response in Restful client:



"data": [

"id": "1",
"title": "appt1",
"start": "2013-12-05 14:00:00",
"end": "2013-12-05 15:00:00",
"backgroundColor": "#00ff00",
"allDay": false
,

"id": "2",
"title": "appt2",
"start": "2013-12-05 15:00:00",
"end": "2013-12-05 17:00:00",
"backgroundColor": "#00ff00",
"allDay": false
,

"id": "3",
"title": "appt3",
"start": "2013-12-05 15:00:00",
"end": "2013-12-05 15:00:00",
"backgroundColor": "#ff00ff",
"allDay": false

]





Question: how can I remove key 'data' in the result look like:


[

"id": "1",
"title": "appt1",
"start": "2013-12-05 14:00:00",
"end": "2013-12-05 15:00:00",
"backgroundColor": "#00ff00",
"allDay": false
,

"id": "2",
"title": "appt2",
"start": "2013-12-05 15:00:00",
"end": "2013-12-05 17:00:00",
"backgroundColor": "#00ff00",
"allDay": false
,

"id": "3",
"title": "appt3",
"start": "2013-12-05 15:00:00",
"end": "2013-12-05 15:00:00",
"backgroundColor": "#ff00ff",
"allDay": false

]






possible duplicate of CakePHP array format

– ndm
Dec 5 '13 at 11:04






use api.cakephp.org/2.4/class-JsonView.html link to get @ndm answer with explanation.

– Rajeev Ranjan
Dec 5 '13 at 12:47





2 Answers
2



I am a bit late here.. but anyway, here is what you were looking for:


$this->set([
'data' => $data,
'_serialize' => 'data',
]);
$this->RequestHandler->renderAs($this, 'json');



Remember that JSON is just a string to PHP, so the literal answer to you question is that you could use substr() or preg_match() if you like. This is, of course, not the correct way to do things. The correct way to do this is:


substr()


preg_match()


$data



Voila:


$data = json_decode('
"data": [

"id": "1",
"title": "appt1",
"start": "2013-12-05 14:00:00",
"end": "2013-12-05 15:00:00",
"backgroundColor": "#00ff00",
"allDay": false
,

"id": "2",
"title": "appt2",
"start": "2013-12-05 15:00:00",
"end": "2013-12-05 17:00:00",
"backgroundColor": "#00ff00",
"allDay": false
,

"id": "3",
"title": "appt3",
"start": "2013-12-05 15:00:00",
"end": "2013-12-05 15:00:00",
"backgroundColor": "#ff00ff",
"allDay": false

]
');
$data = json_encode($data['data']);
);






To whomever chose to down vote this without even explaining yourself, run this code, it produces, verbatim, the result the Viet HP asked for. If you're going to downvote a functioning answer, at least have the courtesy to explain yourself.

– Jonline
Dec 5 '13 at 13:44






Sorry about that, I thought it was kinda obvious - check the duplicate question. The snippet itself might work, but it's not applicable in the OPs situation, he's using CakePHP and lets the JSON view generate the JSON data automatically.

– ndm
Dec 5 '13 at 18:09






Thank @ndm about your solution. it worked perfectly

– Viet HP
Mar 21 '14 at 2:30



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ