JSON
JSON(JavaScript Object Notation)是用于交换数据的文本格式,其中的值被组织成花括号包裹的对象和方括号包裹的数组。它由 RFC 8259 和 ECMA-404 标准定义,媒体类型是 application/json。Genesis API 同样以 JSON 响应。
值的类型
| 类型 | 示例 | 在 Python 中 |
|---|---|---|
| 对象 | {"coin": "BTC"} | dict |
| 数组 | [0.11, 0.45] | list |
| 字符串 | "ByBit" | str |
| 数字 | 214, 0.92 | int, float |
| 布尔值 | true, false | True, False |
| 空值 | null | None |
字段名和字符串只能使用双引号。JSON 没有注释,最后一个元素后面多一个逗号会使文档无效。
Genesis API 响应
/arbitrage/spot-spot 接口响应的假设示例:一行数据,部分字段已省略。
{
"success": true,
"data": [
{
"coin": "FUEL",
"exchange_buy": "ByBit",
"exchange_sell": "Gate",
"spread_pct": 0.11,
"deposit_enabled_buy": true,
"volume_24h_sell": null
}
],
"meta": {
"count": 1,
"updated_at": 1783802410,
"freshness_sec": 10,
"plan": "pro"
}
}
Genesis API 的每个成功响应结构都相同:success 是成功标志,data 存放数据,meta 包含条目数、数据生成的 unix 时间、你的套餐下的交付延迟(秒)以及你的套餐。volume_24h_sell 中的 null 表示没有数据,不要把它当成零。详情见响应格式一节。
在 Python 中解析
import os
import requests
response = requests.get(
"https://api.genesisarbitrage.com/v1/arbitrage/spot-spot",
headers={"X-API-Key": os.environ["GENESIS_API_KEY"]},
params={"min_spread": 0.5},
timeout=10,
)
body = response.json() # JSON → dict
if not body["success"]:
print(body["error"]["code"], body["error"]["message"])
else:
for row in body["data"]:
vol = row["volume_24h_sell"] # null → None
vol = "n/a" if vol is None else f"${vol:,.0f}"
print(row["coin"], f"{row['spread_pct']}%", vol)
response.json() 会把响应文本转换成 Python 字典。如果响应体不是 JSON,例如代理返回的错误页面,该方法会抛出 requests.exceptions.JSONDecodeError。解析成功不等于请求成功:Genesis API 的错误同样以 JSON 形式返回,带有 "success": false 和一个 error 对象,错误代码见错误一节。
另见:REST API、价差、响应格式、ETag 与错误、课程:用 HTTP、请求头和 JSON 发出你的第一个 API 请求。
常见问题
如何把 updated_at 转换成日期?
它是以秒为单位的 unix 时间。在 Python 中,datetime.fromtimestamp(1783802410, timezone.utc) 得到 2026 年 7 月 11 日 20:40:10 UTC。