Gin 中接收并绑定 post 方式传递的 json 数据

第一种(推荐):

type User struct {
    Name string `json:"name"`
    Password int64 `json:"password"`
}
func Login(c *gin.Context) {
    json := User{}

    c.BindJSON(&json)

    log.Printf("%v",&json)
    c.JSON(http.StatusOK, gin.H{
        "name": json.Name,
        "password": json.Password,
    })
}

第二种:

func Login(c *gin.Context) {
    json := make(map[string]interface{}) // 注意该结构接受的内容
    c.BindJSON(&json)
    log.Printf("%v",&json)
    c.JSON(http.StatusOK, gin.H{
        "name": json["name"],
        "password": json["password"],
    })
}
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!