客户端输出

    输出JSON

    1. dispatcher.Router.GET("/json", func(ctx *httpdispatcher.Context) error {
    2. //定义json结构体
    3. var resp struct{
    4. ID int `json:"id"`
    5. Nickname string `json:"nickname"`
    6. //赋值json数据
    7. resp.ID = 123
    8. resp.Nickname = "dxvgef"
    9. //序列化json
    10. data, err := json.Marshal(&resp)
    11. if err != nil {
    12. return ctx.Return(err)
    13. //设置头信息中的文档类型及编码
    14. ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=UTF-8")
    15. //设置HTTP状态码
    16. ctx.ResponseWriter.WriteHeader(200)
    17. //输出JSON数据
    18. ctx.ResponseWriter.Write(data)
    19. return nil