struct转map[string]interface{} int类型变成float64解决方案

type User struct {
    Name string `json:"name"`
    Age int `json:"age"`
}

func main() {
    user := &User{
        Name: "Andy",
        Age:  10,
    }
    u, _ := json.Marshal(user)
    var m map[string]interface{}

    err := json.Unmarshal(u, &m)
    if err != nil {
        fmt.Println("转换失败. err: ", err)
        return
    }

    fmt.Println(m)
    fmt.Printf("age type %T \n", m["age"])
    age := int(m["age"].(float64))
    fmt.Printf("age type %T \n", age)
}

结果:

map[age:10 name:Andy]
age type float64
age type int


你可能感兴趣的:(struct转map[string]interface{} int类型变成float64解决方案)