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}]}"}
相关推荐
for_ever_love__20 分钟前
python基础语法学习: requirements.txt
开发语言·python·学习
程序员清风22 分钟前
FastAPI 入门:用 Python 快速开发现代后端服务
python·oracle·fastapi
SunnyDays101131 分钟前
使用 Python 提取 Word 文档中的表格数据
python·word
for_ever_love__32 分钟前
python基础语法学习: 动态类型
开发语言·python·学习
troy1281 小时前
分布式系统框架选型指南:主流框架优缺点深度对比
python·django·pygame
129Lab1 小时前
BMS算法快速原型开发:AI辅助实现扩展卡尔曼滤波(EKF)SOC估算从理论到代码落地
python·嵌入式开发·bms·卡尔曼滤波·电池管理系统·soc估算
萧鼎1 小时前
safetensors 库的安装、核心语法与实战用法,安全保存模型权重
python·开源·教程·python库·safetensors
千里码aicood1 小时前
基于Python语言的测量程序设计
开发语言·python
2601_962300812 小时前
用PyTorch Lightning简化空间分析:Python和机器学习的力量
python·深度学习·机器学习·空间分析·pytorchlightning
jayson.h2 小时前
python-可视化提取表格-通用
开发语言·python