Zend Framework JSON Output Zend Framework JSON Output ajax ajax

Zend Framework JSON Output


In controller (http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.json):

$this->getHelper('json')->sendJson(array(    'param1' => 'v1'    'param2' => 'v2'));

In view (http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.json):

<?phpecho $this->json(array(    'param1' => 'v1'    'param2' => 'v2'));?>


json is a encoded string containing vars in js style if you need to access the member in this string you need to json_decode the string so

$result = json_decode($jsonString);

but note that json treat php associative array like php object ... so if you pass an array you can access it as $result->memberReference not $result['memberReference'];


The easiest way is to stop view from being executed:

function jsonAction () {    ....    print $json;    exit;}

Also see check http://pl.php.net/json_encode if you don't have JSON string already.