# 类型错误：datetime 类型的对象无法进行 JSON 序列化

- **ID:** `python/starlette-json-response-non-serializable`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 Starlette 的 JSONResponse 中直接返回了 datetime 对象，而标准 JSON 序列化不支持该类型。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   data = {'time': datetime.now().isoformat()}
return JSONResponse(data)
   ```
2. **** (90% 成功率)
   ```
   from starlette.responses import JSONResponse
class CustomJSONResponse(JSONResponse):
    def render(self, content):
        return json.dumps(content, default=str).encode()
   ```

## 无效尝试

- **** — 虽然能序列化，但格式不可控，前端解析困难。 (60% 失败率)
- **** — Starlette 的 JSONResponse 不接受自定义序列化器，此方法无效。 (80% 失败率)
