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}]}"}
相关推荐
梨子串桃子_4 小时前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
文言一心5 小时前
LINUX离线升级 Python 至 3.11.9 操作手册
linux·运维·python
诗词在线5 小时前
中国古代诗词名句按主题分类有哪些?(爱国 / 思乡 / 送别)
人工智能·python·分类·数据挖掘
高锰酸钾_5 小时前
机器学习-L1正则化和L2正则化解决过拟合问题
人工智能·python·机器学习
天天睡大觉6 小时前
Python学习11
网络·python·学习
智航GIS6 小时前
11.11 Pandas性能革命:向量化操作与内存优化实战指南
python·pandas
wtsolutions6 小时前
JSON to Excel Add-in - Seamless Integration Within Excel
json·excel
wtsolutions6 小时前
Getting Started with JSON to Excel Web App - Convert in Seconds
json·excel·web app
写代码的【黑咖啡】6 小时前
Python中的Selenium:强大的浏览器自动化工具
python·selenium·自动化