python - 如何在 python JSON 字符串中转义 true/false bool 值

标签 python json

我有以下代码

headers = {'Content-Type': 'application/json', 'cwauth-token': token}

payload = {'namePostfix': 'test99682', 'costModel': 'NOT_TRACKED', 'clickRedirectType': 'REGULAR', 'trafficSource':{'id': '3a7ff9ec-19af-4996-94c1-7f33e036e7af'}, 'redirectTarget': 'DIRECT_URL', 'client':{'id': 'clentIDc', 'clientCode': 'xxx', 'mainDomain': 'domain.tld', 'defaultDomain': 'domain.tld'', 'dmrDomain': 'domain.tld'', 'customParam1Available': false, 'realtimeRoutingAPI': false, 'rootRedirect': false}, 'country':{'code': 'UK'}, 'directRedirectUrl': 'http://google.co.uk'}

r = requests.post('http://stackoverflow.com', json=payload, headers=headers)

当我点击开始时,它给出了错误

NameError: name 'false' is not defined

我如何在有效负载处转义那些错误的 bool 值?

最佳答案

Python 不使用 false,它使用 False,因此您会收到 NameError,因为 Python 正在寻找名为的变量false 不存在。

将字典中的 false 替换为 False。您在某些地方也有太多引用,因此我删除了这些内容:

payload = {'namePostfix': 'test99682', 'costModel': 'NOT_TRACKED', 'clickRedirectType': 'REGULAR', 'trafficSource':{'id': '3a7ff9ec-19af-4996-94c1-7f33e036e7af'}, 'redirectTarget': 'DIRECT_URL', 'client':{'id': 'clentIDc', 'clientCode': 'xxx', 'mainDomain': 'domain.tld', 'defaultDomain': 'domain.tld', 'dmrDomain': 'domain.tld', 'customParam1Available': False, 'realtimeRoutingAPI': False, 'rootRedirect': False}, 'country':{'code': 'UK'}, 'directRedirectUrl': 'http://google.co.uk'}

同样,相反的 bool 值是 True(不是 true),“null”数据类型是 None

关于python - 如何在 python JSON 字符串中转义 true/false bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40597666/

相关文章:

python - 如何在pycharm中为Django创建一个临时文件

python: += s, 中的逗号有什么作用?

python - SQLAlchemy 查询显示错误 "Can' t join table/selectable 'workflows' to itself”

python - 如何让 turtle 认出一个圆圈?

json - jq - 按值排序

python - 使用 beautifulsoup 从 craigslist 获取价格

c# POST JSON as camel case for all requests

javascript - 如何不编码unicode,javascript

json - 从键值对中提取 "filtering"JSON 对象的最有效方法是什么?

javascript - 如何在 Vue.js 中使用 v-for 遍历对象数组?