go
data_error
ai_generated
true
json: cannot unmarshal number into Go struct field User.age of type string
ID: go/json-cannot-unmarshal-number-into-string
80%Fix Rate
87%Confidence
0Evidence
2024-05-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
The JSON payload contains a numeric value where the Go struct expects a string. The API changed, or the schema was misdeclared.
generic中文
JSON 负载在 Go 结构体期望 string 的位置包含了数字。可能是 API 变更或结构体字段声明错误。
Workarounds
-
88% success
type User struct { Age json.Number `json:"age"` } dec := json.NewDecoder(r) dec.UseNumber() dec.Decode(&u) -
85% success
Define a custom type with UnmarshalJSON that accepts both string and number.
Dead Ends
Common approaches that don't work:
-
60% fail
Loses type safety and just moves the panic downstream when the value is used.
-
75% fail
Leaves partially populated struct and hides real data corruption.