Zend Framework中Json怎么用

2023-05-18,,

这篇文章主要介绍Zend Framework中Json怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体如下:

Json的使用较简单,以下是文档给出的用法:

Json 用来解码和发送 JSON 响应;

当处理期望数据表响应的 AJAX 请求,JSON 响应迅速变成选择的响应。

JSON 可以立即在客户端被解析,从而快速执行。

JSON 动作助手完成以下任务:

如果布局是打开(enabled)的,则关闭(disable)它。

如果视图解析器(ViewRenderer)是打开的,则关闭它。

设置 'Content-Type' 响应头为 'application/json'。

缺省地,不需要等待动作执行完成,立即返回响应。

用法很简单:或者把它作为助手代理的方法来调用,或者调用 encodeJson() 和 sendJson() 方法的其中之一:

class FooController extends Zend_Controller_Action
{
  public function barAction()
  {
    // do some processing...
    // Send the JSON response:
    $this->_helper->json($data);
    // or...
    $this->_helper->json->sendJson($data);
    // or retrieve the json:
    $json = $this->_helper->json->encodeJson($data);
  }
}

Note: 保持布局 (Keeping Layouts)

如果你为 JSON 响应有分离的布局 - 也许把 JSON 封装到一些上下文 - 在 JSON 助手的每个方法接受第二个可选的参数:打开或关闭布局的 flag ,传递一个布尔 true 值将使布局保持打开:

class FooController extends Zend_Controller_Action
{
  public function barAction()
  {
    // Retrieve the json, keeping layouts:
    $json = $this->_helper->json->encodeJson($data, true);
  }
}

以上是“Zend Framework中Json怎么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注本站行业资讯频道!

《Zend Framework中Json怎么用.doc》

下载本文的Word格式文档,以方便收藏与打印。