# requests.exceptions.JSONDecodeError: 期望值：第1行第1列（字符0），解析来自'https://api.example.com/v1/data'的响应时出错

- **ID:** `python/requests-jsondecodeerror-invalid-response`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

服务器返回了非JSON响应（例如HTML错误页面），但客户端期望JSON。

## 版本兼容性

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

## 解决方案

1. **Check the response status code and content type before parsing JSON** (95% 成功率)
   ```
   response = requests.get('https://api.example.com/v1/data')
if response.status_code == 200 and 'application/json' in response.headers.get('Content-Type', ''):
    data = response.json()
else:
    print('Unexpected response:', response.text)
   ```
2. **Use response.raise_for_status() to catch HTTP errors before parsing** (90% 成功率)
   ```
   response = requests.get('https://api.example.com/v1/data')
response.raise_for_status()
data = response.json()
   ```

## 无效尝试

- **Using json.loads() directly on response.text without checking status** — The response may be an error page, not JSON, causing the same decode error. (80% 失败率)
- **Setting a custom decoder with strict=False** — The response is not JSON at all, so no decoder can parse it. (90% 失败率)
