package routers import ( "mybeego/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/data", &controllers.DataController{}) }
package controllers import ( "github.com/astaxie/beego" ) type DataController struct { beego.Controller } type LIKE struct { Food string Watch string Listen string } type JSONS struct { //必須的大寫開頭 Code string Msg string User []string `json:"user_info"`//key重命名,最外面是反引號 Like LIKE } func (c *DataController) Get() { data := &JSONS{"100", "獲取成功", []string{"maple","18"},LIKE{"蛋糕","電影","音樂"}} c.Data["json"] = data c.ServeJSON() }
{ "Code": "100", "Msg": "獲取成功", "User": [ "maple", "18" ], "Like": { "Food": "蛋糕", "Watch": "電影", "Listen": "音樂" } }