go
encoding_error
ai_generated
true
json:意外的 JSON 输入结束
json: unexpected end of JSON input
ID: go/json-unexpected-end-of-json-input
80%修复率
85%置信度
1证据数
2023-09-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| go1.21 | active | — | — | — |
| go1.22 | active | — | — | — |
| go1.23 | active | — | — | — |
根因分析
JSON 解码器在完成有效 JSON 值的解析之前遇到了输入流的结束。
English
The JSON decoder encountered the end of the input stream before completing the parsing of a valid JSON value.
官方文档
https://pkg.go.dev/encoding/json#Decoder.Decode解决方案
-
Check the source of the JSON data to ensure it is complete (e.g., full HTTP response body, complete file read). Use io.ReadAll to read all data before unmarshaling.
-
Use json.Decoder with a buffered reader and check for io.ErrUnexpectedEOF specifically to provide a better error message.
无效尝试
常见但无效的做法:
-
Adding more fields to the Go struct to match the expected JSON.
80% 失败
The error is about incomplete input, not missing fields; adding fields doesn't fix malformed or truncated data.
-
Using json.Unmarshal with a string that includes extra whitespace.
70% 失败
Whitespace is ignored by the decoder; the input must be a complete JSON value.