这可能非常简单。我正在玩webhooks,我的一个测试抛出了一个JSON字典数据,我正试图将它复制/粘贴到我的终端并打印出来。然而,我得到了错误。为什么?请帮帮忙。
json.loads({"signature": {"timestamp": "1542320326", "token": "78b89c864547e371f7d708fcde9ccf3df937ce0e296cff8683", "signature": "822ae5f14a85dc25dacfd53a7ab1d55f03529aae0e8535d29758740924fde385"}, "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp": 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"}, "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [], "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2": "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers": {"to": "Alice <alice@example.com>", "message-id": "20110215055645.25246.63817@biennial-dot-filings.us", "from": "Bob <bob@biennial-dot-filings.us>", "subject": "Test complained webhook"}, "attachments": [], "size": 111}, "recipient": "alice@example.com", "event": "complained"}})回溯(最近一次调用):文件"",第1行,在NameError中:名称'false‘未定义
发布于 2018-11-16 06:34:11
在Python中,false不是有效的类型/表达式。我想你想要的是False。你可以阅读更多的here。
正如@Uku提到的,你可以使用json.loads()来处理这个问题。
发布于 2018-11-16 06:35:16
Json不直接映射到Python数据结构。
你必须做json.loads("your string")。在JSON中是false,在Python中我们有False
例如
json.loads('{"signature": {"timestamp": "1542320326", "token": "78b89c864547e371f7d708fcde9ccf3df937ce0e296cff8683", "signature": "822ae5f14a85dc25dacfd53a7ab1d55f03529aae0e8535d29758740924fde385"}, "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp": 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"}, "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [], "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2": "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers": {"to": "Alice <alice@example.com>", "message-id": "20110215055645.25246.63817@biennial-dot-filings.us", "from": "Bob <bob@biennial-dot-filings.us>", "subject": "Test complained webhook"}, "attachments": [], "size": 111}, "recipient": "alice@example.com", "event": "complained"}}')发布于 2018-11-16 07:25:33
json.loads需要一个字符串作为其参数。为了使复制到Python中的有效字符串中的JSON对象成为文字,需要用引号将其括起来。
由于JSON字符串本身包含"字符,因此必须使用'
json.loads('{"signature": {"timestamp": "1542320326", ... }}')https://stackoverflow.com/questions/53328810
复制相似问题