go
type_error
ai_generated
true
json: cannot unmarshal number into Go value of type string
ID: go/json-unmarshal-type-mismatch
80%Fix Rate
90%Confidence
0Evidence
2024-11-03First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.14 | active | — | — | — |
| 1.15 | active | — | — | — |
| 1.16 | active | — | — | — |
Root Cause
The JSON data contains a value of a different type than the target Go field, such as a number where a string is expected.
generic中文
JSON 数据包含与目标 Go 字段类型不同的值,例如在期望字符串的地方出现了数字。
Workarounds
-
80% success
Define the struct field as interface{} and then type assert. -
85% success
Use a custom UnmarshalJSON method to handle type conversion.
Dead Ends
Common approaches that don't work:
-
70% fail
This only works if the field is of type json.Number, not string.
-
90% fail
Tags do not change the type conversion.