Sono nuovo in Golang e sto cercando di ottenere un valore da un JSON e lo lancio su int ma non funziona. Non so come farlo correttamente.converte l'interfaccia {} in int in Golang
Ecco il messaggio di errore:
...cannot convert val (type interface {}) to type int: need type assertion
e al codice:
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
qualsiasi aiuto sarebbe apprezzato.
Buona risposta! Le specifiche della lingua sono sempre il posto migliore per cercare una risposta! –
Grazie. è una bella risposta. – Muktadir
Non la risposta corretta per l'input JSON. Vedi sotto. – Liam