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
]
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.
possible duplicate of CakePHP array format
– ndm
Dec 5 '13 at 11:04