ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
json格式数据输出 通过把要输出的数据放到Data["json"]中,然后调用ServeJSON()进行渲染,就可以把数据进行JSON序列化输出。 ~~~ |-- admin | |--controllers | `-- user.go ~~~ ~~~ package admin import ( "github.com/astaxie/beego" ) type UserController struct { beego.Controller } type stu struct { Name string Age int Addr string } func (this *UserController) Index() { user := &stu{"Murphy", 28, "帝都"} this.Data["json"] = user this.ServeJSON() this.TplName = "admin/user/index.html" } ~~~ ~~~ |-- views | |--admin | |--user | `-- index.html ~~~ ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h3>{{.json}}</h3> </body> </html> ~~~ 浏览器访问: http://127.0.0.1:8080/admin/user/index 浏览器输出: ~~~ { "Name": "Murphy", "Age": 28, "Addr": "帝都" } ~~~