# 错误：json：调用time.Duration类型的MarshalJSON时出错

- **ID:** `go/encoding-json-marshal-time-duration`
- **领域:** go
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

time.Duration未实现json.Marshaler，会被编组为纳秒整数，可能不符合预期。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Create a custom type that marshals duration as string** (90% 成功率)
   ```
   type Duration time.Duration
func (d Duration) MarshalJSON() ([]byte, error) {
    return json.Marshal(time.Duration(d).String())
}
   ```

## 无效尝试

- **Using string formatting to convert duration** — Inconsistent output; use custom type with MarshalJSON. (60% 失败率)
