go
type_error
ai_generated
true
json: cannot unmarshal array into Go value of type struct { Name string }
ID: go/json-cannot-unmarshal-array-into-struct
80%Fix Rate
88%Confidence
0Evidence
2024-03-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
Root Cause
JSON data is an array but target Go type is a struct, causing type mismatch.
generic中文
JSON 数据是数组,但目标 Go 类型是结构体,导致类型不匹配。
Workarounds
-
95% success
Unmarshal into []struct{ Name string } if JSON is array: var data []struct{Name string}; json.Unmarshal([]byte(jsonStr), &data) -
85% success
Use json.RawMessage to inspect first, then unmarshal accordingly.
Dead Ends
Common approaches that don't work:
-
90% fail
Data not parsed, leads to nil values.
-
40% fail
May not match API contract; breaks other code.