JSON JSON responses are rapidly becoming the response of choice when dealing with AJAX requests that expect dataset responses; JSON can be immediately parsed on the client-side, leading to quick execution. Usage Usage is simple: either call it as a method of the helper broker, or call one of the methods encodeJson() or sendJson(): direct($data, $sendNow = true, $keepLayouts = false, $encodeData = true) sendJson($data, $keepLayouts = false, $encodeData = true) encodeJson($data, $keepLayouts = false, $encodeData = true) $data: data to encode as JSON $sendNow: flag to define whether to send the JSON data immediately. When true, the helper will immediately set the respose body and exit. $keepLayouts: flag to define whether to enable or disable layours. When false, all layouts are disabled. Optionally, this can be an array of options to pass as the second argument to Zend_Json::encode(). This array of options allows enabling layouts and encoding using Zend_Json_Expr. $encodeData: flag to define whether $data is already JSON-encoded. When true, this helper will not encode $data to JSON before sending. Keeping Layouts If you have a separate layout for JSON responses -- perhaps to wrap the JSON response in some sort of context -- each method in the JSON helper accepts an optional argument $keepLayouts: a flag to enable or disable layouts. Passing a boolean TRUE value will keep layouts enabled: _helper->json($data, true); ]]> Optionally, you can pass an array as the third parameter. This array may contain a variety of options, including the keepLayouts option: _helper->json($data, true, array('keepLayouts' => true); // ...or, call a method of the helper $this->_helper->sendJson($data, array('keepLayouts' => true)); ]]> Enabling encoding using Zend_Json_Expr Zend_Json::encode() allows the encoding of native JSON expressions using Zend_Json_Expr objects. This option is disabled by default. To enable this option, pass a boolean TRUE value to the enableJsonExprFinder option: _helper->json($data, true, array('enableJsonExprFinder' => true); ]]> If you desire to do this, you must pass an array as the third argument. This also allows you to combine other options, such as the keepLayouts option. All such options are then passed to Zend_Json::encode(). _helper->json($data, true, array( 'enableJsonExprFinder' => true, 'keepLayouts' => true, )); ]]> Example _helper->json($data); // or... $this->_helper->json->sendJson($data); // or retrieve the json: $json = $this->_helper->json->encodeJson($data); } } ]]>