beego json mysql_golang 框架 beego ServeJSON的数据到底应该用什么类型?

我没有太理解你的问题, 你是想问 c.Data["json"] 是 JSONStruct的值和指针的区别吗, 为什么用指针和值都可以得到正确的输出?这个问题我建议你看 c.ServeJSON()的源码就明白了

// ServeJson sends a json response with encoding charset.

func (c *Controller) ServeJson(encoding ...bool) {

var hasIndent bool

var hasencoding bool

if RunMode == "prod" {

hasIndent = false

} else {

hasIndent = true

}

if len(encoding) > 0 && encoding[0] == true {

hasencoding = true

}

c.Ctx.Output.Json(c.Data["json"], hasIndent, hasencoding)

}

// Json writes json to response body.

// if coding is true, it converts utf-8 to \u0000 type.

func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) error {

output.Header("Content-Type", "application/json; charset=utf-8")

var content []byte

var err error

if hasIndent {

content, err = json.MarshalIndent(data, "", " ")

} else {

content, err = json.Marshal(data)

}

if err != nil {

http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)

return err

}

if coding {

content = []byte(stringsToJson(string(content)))

}

output.Body(content)

return nil

}

实际这个方法是对json序列化和 http 响应的封装, 只是直接把 c.Data["json"] 传递给json组件了而已, json 内部会进行反射, 会自动处理指针类型的数据,和我们平时使用没有任何区别, 只要是 json组件可以序列化的数据都可以给 c.Data["json"] ,而 c.Data 是 map[interface{}]interface{} 类型的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值