这可能非常简单。我正在玩Webhooks,我的一个测试抛出了JSON dict数据,我试图将其复制/粘贴到终端并进行pprint。但是我遇到了错误。为什么?请帮忙。
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'未定义
答案 0 :(得分:0)
在Python中,false
不是有效的类型/表达式。我认为您想要的是False
。您可以阅读更多here。
如@Uku所述,您可以使用json.loads()
来解决此问题。
答案 1 :(得分:0)
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"}}')
答案 2 :(得分:0)
json.loads
需要一个字符串作为其参数。为了使您将JSON对象复制到Python中的有效字符串文字中,您需要将其用引号引起来。
由于JSON字符串本身包含"
字符,因此您必须使用'
:
json.loads('{"signature": {"timestamp": "1542320326", ... }}')