− Table of content
JSON is a data format using javascript syntax techniques. this ease greatly the use of JSON encoded data in HTML content.
To send JSON data, there is a specific jResponseJson. Its alias is "json". All you need to do is assign your encoded json to $datas:
$rep = $this->getResponse('json');
$rep->datas = array( 'lastname'=>'dupont', 'firstname'=>'jean');
the client will receive:
{ nom: 'dupont', prenom:'jean'}
a use case combined with jDao ¶
$countryDao = jDao::get('common~country');
$countries = $countryDao->findAll();
$response = $this->getResponse('json');
$response->datas = array();
foreach($countries as $country) {
$response->datas[] = array('id' => $country->id , 'name' => $country->name);
}
the client will receive:
{ id: '1', name:'Europe'}