gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:47:14 25 4
gpt4 key购买 nike

我有以下代码

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com