zoukankan      html  css  js  c++  java
  • beego——多种格式的数据输出

    beego当初设计的时候就考虑了API功能的设计,而我们在设计API的时候经常是输出JSON或者XML数据,那么beego提供了这样的方式直接输出:

    1.JSON格式输出

    func (this *AddController) Get() {
        mystruct := { ... }
        this.Data["json"] = &mystruct
        this.ServeJSON()
    }

    调用ServerJSON之后,会设置content-type 为 application/json,然后同时把数据进行 JSON 序列化输出。

    2.XML格式输出

    func (this *AddController) Get() {
        mystruct := { ... }
        this.Data["xml"]=&mystruct
        this.ServeXML()
    }
    

    调用 ServeXML 之后,会设置 content-type 为 application/xml,同时数据会进行 XML 序列化输出。

    3.jsonp调用

    func (this *AddController) Get() {
        mystruct := { ... }
        this.Data["jsonp"] = &mystruct
        this.ServeJSONP()
    }
    

    调用 ServeJSONP 之后,会设置 content-type 为 application/javascript,然后同时把数据进行 JSON 序列化,然后根据请求的 callback 参数设置 jsonp 输出。

    开发模式下序列化后输出的是格式化易阅读的 JSON 或 XML 字符串;在生产模式下序列化后输出的是压缩的字符串。

  • 相关阅读:
    mdadm
    RAID磁盘阵列学习笔记
    内存究竟有多快?
    fping
    Intel® RAID Software User’s Guide
    为什么寄存器比内存快?
    OC-Category
    OC-id、构造方法
    OC- @property @synthesize
    OC-点语法
  • 原文地址:https://www.cnblogs.com/yangmingxianshen/p/10122392.html
Copyright © 2011-2022 走看看