我有一个来自api as map[message:Login Success. userid:1]
的json响应.
服务器:
c.JSON(200, gin.H{"message": "Login Success.", "userid": 1})
客户端:
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
msg, ok := result["message"].(string)
if !ok {
msg = "Something went wrong."
}
userID, ok := result["userid"].(int)
if !ok {
userID = 0
}
但是userID, ok := result["userid"].(int)
总是失败的.我甚至试过使用:
switch v := x.(type) {
case nil:
fmt.Println("x is nil")
case int:
fmt.Println("x is", v)
case bool, string:
fmt.Println("x is bool or string")
default:
fmt.Println("type unknown")
}
它只给了我unknown美元.为什么整数不被视为整数?
看起来它将该值视为float64
.