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}]}"}
相关推荐
Hylan_J3 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
莫忘初心丶3 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
失败尽常态5236 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447746 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农6 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿6 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing7 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
xinxiyinhe8 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语8 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3
橘子师兄9 小时前
分页功能组件开发
数据库·python·django