# json: 不支持的类型：map[int]string

- **ID:** `go/json-marshal-error-unsupported-type`
- **领域:** go
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

尝试编组键不是字符串的 Go 类型，JSON 格式不支持（JSON 对象键必须是字符串）。

## 版本兼容性

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

## 解决方案

1. ```
   Convert map keys to strings before marshaling: `newMap := make(map[string]string); for k, v := range oldMap { newMap[fmt.Sprintf("%d", k)] = v }`
   ```
2. ```
   Define a custom struct or use a slice of key-value pairs instead: `type Entry struct { Key int; Value string }` then marshal `[]Entry`.
   ```

## 无效尝试

- **** — MarshalIndent also calls json.Marshal internally; same error occurs. (90% 失败率)
- **** — Returning nil causes json.Marshal to serialize the value as 'null', not skip it; error still may occur depending on implementation. (60% 失败率)
