将 interface{} 转换为 int

我正在尝试从 JSON 中获取一个值并将其转换为 int 但它不起作用,我不知道如何正确执行。


这是错误消息:


...cannot convert val (type interface {}) to type int: need type assertion

和代码:


    var f interface{}

    err = json.Unmarshal([]byte(jsonStr), &f)

    if err != nil {

        utility.CreateErrorResponse(w, "Error: failed to parse JSON data.")

        return

    }


    m := f.(map[string]interface{})


    val, ok := m["area_id"]

    if !ok {

        utility.CreateErrorResponse(w, "Error: Area ID is missing from submitted data.")

        return

    }


    fmt.Fprintf(w, "Type = %v", val)   // <--- Type = float64

    iAreaId := int(val)                // <--- Error on this line.

    testName := "Area_" + iAreaId      // not reaching here


绝地无双
浏览 383回答 3
3回答

米脂

我假设:如果您通过浏览器发送 JSON 值,那么您发送的任何数字都将是 float64 类型,因此您无法在 golang 中直接获取该值。所以做这样的转换://As that says:&nbsp;fmt.Fprintf(w, "Type = %v", val) // <--- Type = float64var iAreaId int = int(val.(float64))这样你就可以得到你想要的确切价值。

不负相思意

添加另一个使用的答案switch......那里有更全面的例子,但这会给你这个想法。例如,t成为每个case范围内的指定数据类型。请注意,您必须case在一个类型中仅提供一种类型,否则t仍然是interface.package mainimport "fmt"func main() {&nbsp; &nbsp; var val interface{} // your starting value&nbsp; &nbsp; val = 4&nbsp; &nbsp; var i int // your final value&nbsp; &nbsp; switch t := val.(type) {&nbsp; &nbsp; case int:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = t&nbsp; &nbsp; case int8:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case int16:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case int32:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case int64:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case bool:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%t == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; // // not covertible unless...&nbsp; &nbsp; &nbsp; &nbsp; // if t {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; i = 1&nbsp; &nbsp; &nbsp; &nbsp; // } else {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; i = 0&nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; case float32:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%g == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case float64:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%f == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case uint8:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case uint16:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case uint32:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case uint64:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; i = int(t) // standardizes across systems&nbsp; &nbsp; case string:&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%s == %T\n", t, t)&nbsp; &nbsp; &nbsp; &nbsp; // gets a little messy...&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; // what is it then?&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%v == %T\n", t, t)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("i == %d\n", i)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go