You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python构造JSON请求:如何让timestamp字段为数字类型(无引号)

解决JSON-RPC请求中timestamp字段为数字类型的问题

问题根源

你当前代码里通过str()将timestamp值转为字符串,还额外拼接空字符串,导致start_timestamp和end_timestamp成为字符串类型,JSON序列化后自然会带上引号。

修正后的代码

如果是硬编码的timestamp,直接使用数字即可:

msg_1 = {
    "jsonrpc": "2.0",
    "id": 833,
    "method": "public/get_tradingview_chart_data",
    "params": {
        "instrument_name": str(instr_),
        "start_timestamp": 1554373800000,
        "end_timestamp": 1654373800000,
        "resolution": "1440"
    }
}

如果timestamp来自字符串类型的变量,需要先转为整数类型:

# 示例:假设start_ts和end_ts是字符串格式的timestamp
start_timestamp = int(start_ts)
end_timestamp = int(end_ts)

msg_1 = {
    "jsonrpc": "2.0",
    "id": 833,
    "method": "public/get_tradingview_chart_data",
    "params": {
        "instrument_name": str(instr_),
        "start_timestamp": start_timestamp,
        "end_timestamp": end_timestamp,
        "resolution": "1440"
    }
}

验证效果

修正后,字典中的start_timestamp和end_timestamp为整数类型,序列化后的JSON会保持数字格式,不带引号:

{
  "jsonrpc": "2.0",
  "id": 833,
  "method": "public/get_tradingview_chart_data",
  "params": {
    "instrument_name": "ETH-12AUG22",
    "start_timestamp": 1554373800000,
    "end_timestamp": 1654373800000,
    "resolution": "1440"
  }
}

内容的提问来源于stack exchange,提问作者Kiann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 15:09:12