# json：意外的 JSON 输入结束

- **ID:** `go/json-unexpected-end-of-json-input`
- **领域:** go
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

JSON 解码器在完成有效 JSON 值的解析之前遇到了输入流的结束。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| go1.21 | active | — | — |
| go1.22 | active | — | — |
| go1.23 | active | — | — |

## 解决方案

1. ```
   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.
   ```
2. ```
   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.** — The error is about incomplete input, not missing fields; adding fields doesn't fix malformed or truncated data. (80% 失败率)
- **Using json.Unmarshal with a string that includes extra whitespace.** — Whitespace is ignored by the decoder; the input must be a complete JSON value. (70% 失败率)
