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}]}"}
相关推荐
knight_9___10 分钟前
大模型project面试4
人工智能·python·深度学习·算法·面试·agent
m0_5913647313 分钟前
Go语言怎么做链路追踪_Go语言分布式链路追踪教程【精选】
jvm·数据库·python
m0_4636722015 分钟前
HTML函数工具是否支持雷蛇等游戏外设_RGB同步汇总【汇总】
jvm·数据库·python
zkkkkkkkkkkkkk20 分钟前
python使用celery实现异步任务
redis·python·rabbitmq·rocketmq
iAm_Ike36 分钟前
如何用 IndexedDB 存储从 API 获取的超大列表并实现二级索引
jvm·数据库·python
Land032939 分钟前
指纹浏览器自动化集成方案|多浏览器RPA适配实战记录
运维·人工智能·爬虫·python·selenium·自动化·rpa
X56611 小时前
CSS Flex布局如何让特定子元素不参与缩放_设置flex-shrink- 0的实战技巧
jvm·数据库·python
databook1 小时前
告别手动计算,SymPy 初识与 Manim 联动
python·数学·动效
jayson.h1 小时前
可视化界面
开发语言·python
weixin_444012931 小时前
CSS如何快速实现网站换肤功能_利用CSS变量重置全局颜色方案
jvm·数据库·python