package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
func getJson(ctx *gin.Context) {
type user struct {
Username string `json:"username"`
Password string `json:"password"`
}
jsondata := user{}
err := ctx.BindJSON(&jsondata)
if err != nil {
panic(err)
}
ctx.JSON(http.StatusOK, gin.H{
"username": jsondata.Username,
"password": jsondata.Password,
})
}
func main() {
router := gin.Default()
router.GET("/JSON", getJson)
err := router.Run(":8080")
if err != nil {
fmt.Println(err)
return
}
}
Gin获取json数据