# json：无法将数字反序列化到 Go 结构体字段 User.age（类型 string）

- **ID:** `go/json-cannot-unmarshal-number-into-string`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

JSON 负载在 Go 结构体期望 string 的位置包含了数字。可能是 API 变更或结构体字段声明错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.22 | active | — | — |

## 解决方案

1. **** (88% 成功率)
   ```
   type User struct {
  Age json.Number `json:"age"`
}
dec := json.NewDecoder(r)
dec.UseNumber()
dec.Decode(&u)
   ```
2. **** (85% 成功率)
   ```
   Define a custom type with UnmarshalJSON that accepts both string and number.
   ```

## 无效尝试

- **** — Loses type safety and just moves the panic downstream when the value is used. (60% 失败率)
- **** — Leaves partially populated struct and hides real data corruption. (75% 失败率)
