go
type_error
ai_generated
true
json: cannot unmarshal number into Go struct field Age of type string
ID: go/encoding-json-type-mismatch
80%Fix Rate
86%Confidence
0Evidence
2024-03-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
Root Cause
The JSON data contains a numeric value where the Go struct expects a string, causing a type mismatch during unmarshaling.
generic中文
JSON 数据包含数字值,而 Go 结构体期望字符串,导致反序列化时类型不匹配。
Workarounds
-
85% success
type Person struct { Age string `json:"age"` }; var p Person; if err := json.Unmarshal(data, &p); err != nil { ... }; // or use custom UnmarshalJSON
Dead Ends
Common approaches that don't work:
-
80% fail
Loses type safety and requires manual type assertions, increasing complexity.
-
90% fail
Silently drops data, leading to incorrect application behavior.