steve
steve

Reputation: 1856

Pretty, Tidy JSON output using Zend Framework?

I am using the Zend Framework helper for JSON. I am returning an array of data from a domain entity using a created method toArray().

Is there a way to change the format from tidy to pretty?

PHP

$data = array(
            "success"=>true,
            "user"=>array($user->toArray())
            );
$this->_helper->json($data);

Body Response:

{"success":true,"user":[{"id":9,"displayName":"joey","firstName":"joe","lastName":"blow","email":"[email protected]","role":"user"}]}

To:

{
  "success":true,
  "user": [{
     "id":9, 
     "displayName":"joey",
     "firstName":"joe",
     "lastName":"blow",
     "email":"[email protected]",
     "role":"user"
  }]
}

Upvotes: 0

Views: 1155

Answers (2)

Amrish Prajapati
Amrish Prajapati

Reputation: 787

Try this :

$json = Zend_Json::encode($phpNative);
echo Zend_Json::prettyPrint($json, array("indent" => " "));

LINK : http://framework.zend.com/manual/en/zend.json.basics.html

Upvotes: 1

conradfr
conradfr

Reputation: 184

It's not tested and would depend on some of your json implementation but you could try :

$json = Zend_Json::encode($data);
$json = Zend_Json::prettyPrint($json);

$this->_helper->json($json, true, array('encodeData' => false));

Upvotes: 1

Related Questions