0

我正在为 JSON 使用 Zend Framework 助手。我使用创建的方法 toArray() 从域实体返回数据数组。

有没有办法将格式从整洁更改为漂亮?

PHP

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

身体反应:

{"success":true,"user":[{"id":9,"displayName":"joey","firstName":"joe","lastName":"blow","email":"joeblow@yahoo.com","role":"user"}]}

到:

{
  "success":true,
  "user": [{
     "id":9, 
     "displayName":"joey",
     "firstName":"joe",
     "lastName":"blow",
     "email":"joeblow@yahoo.com",
     "role":"user"
  }]
}
4

2 回答 2

1

它未经测试,将取决于您的一些 json 实现,但您可以尝试:

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

$this->_helper->json($json, true, array('encodeData' => false));
于 2012-07-03T09:43:33.037 回答
1

试试这个 :

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

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

于 2012-07-04T10:56:52.803 回答