【问题标题】:Pretty, Tidy JSON output using Zend Framework?使用 Zend 框架的漂亮、整洁的 JSON 输出?
【发布时间】:2012-07-03 05:41:04
【问题描述】:

我正在为 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"
  }]
}

【问题讨论】:

  • 您可以使用 json 格式化程序。甚至可能存在离线版本。向谷歌询问更多信息

标签: json zend-framework helper formatting


【解决方案1】:

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

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

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

【讨论】:

  • 看起来 echo Zend_Json::prettyPrint($data) 是我唯一的选择。谢谢。
【解决方案2】:

试试这个:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 2012-07-13
    • 2012-05-18
    • 2010-10-20
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多