32 lines
616 B
Markdown
32 lines
616 B
Markdown
|
|
JSON渲染
|
|
|
|
```shell
|
|
r.GET("/moreJSON", func(c *gin.Context) {
|
|
// 方法二:使用结构体
|
|
var msg struct {
|
|
Name string `json:"user"`
|
|
Message string
|
|
Age int
|
|
}
|
|
msg.Name = "小王子"
|
|
msg.Message = "Hello world!"
|
|
msg.Age = 18
|
|
c.JSON(http.StatusOK, msg)
|
|
})
|
|
```
|
|
|
|
Yaml
|
|
|
|
```go
|
|
r.GET("/someXML", func(c *gin.Context) {
|
|
c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
|
|
})
|
|
|
|
r.GET("/someYAML", func(c *gin.Context) {
|
|
c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
|
|
})
|
|
```
|
|
|
|
![](http://file.569781231.xyz/building/202206200928541.png)
|