我从 gin doc 中了解到,您可以将 json 绑定到类似的结构
type Login struct {
    User     string `form:"user" json:"user" binding:"required"`
    Password string `form:"password" json:"password" binding:"required"`
}
func main() {
    router := gin.Default()
    // Example for binding JSON ({"user": "manu", "password": "123"})
    router.POST("/loginJSON", func(c *gin.Context) {
        var json Login
        if c.BindJSON(&json) == nil {
            if json.User == "manu" && json.Password == "123" {
                c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
            } else {
                c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
            }
        }
    })
}
您始终必须构建一个结构来绑定 JSON。
但是如果有一个非常复杂的JSON数据,我只需要获取其中的一部分,创建一个复杂的struct是一个很大的负担。可以避免id直接解析吗?