That’s really a good Zend Framework’s class that help you do the encode/decode job very easily. First of all it escapes everything for you and second it prints a correct/valid code. Note that sometimes if you have a trailing whitespace after the closing PHP tag – ?> that will result in an error.
Here’s some code:
public function jsonAction() { $data = array(3,4,'test', 'my-name' => 3,4); echo Zend_Json::encode($data); $this->_helper->viewRenderer->setNoRender(true); $this->view->layout()->disableLayout(); } |
and the result is:
{"0":3,"1":4,"2":"test","my-name":3,"3":4} |
Note that all integers are printed without double quotes – which saves some space!