JSON语法是Python语法的严格子集吗? [英] Is JSON syntax a strict subset of Python syntax?
本文介绍了JSON语法是Python语法的严格子集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JSON与Python语法非常相似.所有JSON对象都能直接无错误地转换为Python吗?
JSON is very similar to Python syntax. Can all JSON objects directly convert to Python without error?
示例
以下是有效的JSON对象:
The following is a valid JSON object:
// Valid JSON
{"foo":"bar"}
该对象将直接转换为具有键"foo"和值"bar"的Python字典:
This object will directly translate to a Python dictionary with key "foo" and value "bar":
# Python
json_dict = eval('{"foo":"bar"}')
推荐答案
否.特别是true
,false
和null
不是Python,尽管它们在Python中确实具有直接等效项(分别为True
,False
和None
).
No. In particular, true
, false
, and null
are not Python, although they do have direct equivalents in Python (True
, False
, and None
respectively).
// Valid JSON
{"sky_is_blue":true}
但是在Python中使用时...
But when used in Python...
# Python
>>> json_dict = eval('{"sky_is_blue":true}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'true' is not defined
这篇关于JSON语法是Python语法的严格子集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文