json.dumps结果不要空格

json.dumps()函数中的separators参数是一个元组,用于指定生成的 JSON 字符串中不同部分的分隔符。元组中包含两个字符串,分别表示键值对之间的分隔符和项之间的分隔符。

默认情况下,separators参数设置为( ",", ":"),即生成的 JSON 字符串中键值对之间使用逗号,分隔,键和值之间使用冒号:分隔。

如果要去掉生成的 JSON 字符串中的空格,可以将separators参数设置为( ",", ":")

python 复制代码
    import json

    input_object = {
"accId": "2023101613595110268484",
"clientId": "2023101613595110268",
"signType": "SHA256",
"sign": "64723141643f5ed6dd1e4e8e698ca822ba8683dd3fb01152656a0f94df23f8b0",
"version": "1.0",
"bizContent": "{\"captureDelayHours\":0,\"amount\":1000,\"currency\":\"TWD\",\"merchantTransactionId\":\"65a501807018de895ad4c7aa\",\"payResultUrl\":\"https://pay.9longe.net/redirect.html\",\"payCancelUrl\":\"https://pay.9longe.net/redirect.html\",\"notificationUrl\":\"http://sit-yao.sit-set:8881/thirdpart/pingpong/notify/104\",\"language\":\"ru\",\"tradeCountry\":\"RU\",\"shopperIP\":\"http://127.0.0.1\",\"merchantUserId\":\"aDT5CUq1Fb3P06MM\",\"payment_methods\":[\"VISA\",\"Mastercard\",\"NSPKMIR\",\"QIWIWALLET\"],\"goods\":[{\"name\":\"testgoods\",\"number\":\"1\",\"unitPrice\":1000}]}"
}


    # 将Python字典转换为JSON格式的字符串,并进行转义,去掉空格
    output_str = json.dumps(input_object, separators=(",", ":"))
    print(output_str)

结果

python 复制代码
{"accId":"2023101613595110268484","clientId":"2023101613595110268","signType":"SHA256","sign":"64723141643f5ed6dd1e4e8e698ca822ba8683dd3fb01152656a0f94df23f8b0","version":"1.0","bizContent":"{\"captureDelayHours\":0,\"amount\":1000,\"currency\":\"TWD\",\"merchantTransactionId\":\"65a501807018de895ad4c7aa\",\"payResultUrl\":\"https://pay.9longe.net/redirect.html\",\"payCancelUrl\":\"https://pay.9longe.net/redirect.html\",\"notificationUrl\":\"http://sit-yao.sit-set:8881/thirdpart/pingpong/notify/104\",\"language\":\"ru\",\"tradeCountry\":\"RU\",\"shopperIP\":\"http://127.0.0.1\",\"merchantUserId\":\"aDT5CUq1Fb3P06MM\",\"payment_methods\":[\"VISA\",\"Mastercard\",\"NSPKMIR\",\"QIWIWALLET\"],\"goods\":[{\"name\":\"testgoods\",\"number\":\"1\",\"unitPrice\":1000}]}"}
相关推荐
Android小码家3 分钟前
framework&framework-minus-apex&android.jar的区别
android·python·jar
lee_curry7 分钟前
第十周完整学习笔记:继续升级为“检索更准、权限安全、索引可维护、失败可恢复、长期可运行”的企业级 RAG 系统
python·ai·langchain·agent·langgraph
Ivanqhz1 小时前
MoE + LLM 应用
数据库·人工智能·pytorch·python·深度学习·算法·1024程序员节
Mr. zhihao1 小时前
Deep Agent 架构深度解析:主Agent与子Agent完整交互机制与核心参数作用
python·ai·langgraph·deepagent
狗凯之家源码网1 小时前
iOS C2 V2 与 Python FastAPI 全链路远控系统深度评测
python·ios·fastapi
超级架构师1 小时前
让 Fortran 模型自己调用 Agent:原生 Fortran 2008 实现深度剖析
人工智能·python·架构·机器人·开源·fortran
小蒜学长1 小时前
基于Django+Vue喵汪联盟宠物领养系统的设计与实现(代码+数据库+LW)
后端·python·django·宠物领养系统·流程规范化
云边有个稻草人2 小时前
Prometheus Pushgateway教程:离线任务指标上报、Shell与Python示例、远程访问
开发语言·python·prometheus
fanruitian2 小时前
pycharm 使用anaconda
ide·python·pycharm
Dwzun2 小时前
基于SpringBoot3+Vue3的油莎豆种植管理系统
java·前端·vue.js·spring boot·后端·python·计算机网络