解析 JSON 接口

45.7 解析 JSON 接口

r.json() 把响应体解析为 dict/list;非 JSON 会抛异常。

先检查 r.status_code == 200 再解析。

调用 JSON API

# ========================================
# 示例:r.json()
# ========================================
import requests

r = requests.get('https://httpbin.org/json', timeout=10)
if r.status_code == 200:
    data = r.json()
    print('键', list(data.keys())[:3])