beego返回json數據

package controllers

import (
	"github.com/astaxie/beego"
)

type UserController struct {
	beego.Controller
}

// 定義返回的結構體,並轉爲json格式
type Result struct {
	Status int `json:"status"` // 首字母大寫
	Msg string `json:"msg"`
}

func (this *UserController) GetMessageCode() {
	result := Result{0, "asd"} // 賦值
	this.Data["json"] = &result 
	this.ServeJSON() // 返回json
}

func (this *UserController) CheckingCode() {
	println(222)
	this.Data["data"] = Result{0, "dddd"}
	this.ServeJSON()
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章