我见过许多项目使用simplejson模块而不是标准库中的json模块。此外,还有许多不同的simplejson模块。为什么要使用这些替代品,而不是标准库中的替代品?
当前回答
一些值在simplejson和json之间的序列化方式不同。
值得注意的是,collections.namedtuple的实例被json序列化为数组,而被simplejson序列化为对象。您可以通过将namedtuple_as_object=False传递给simplejson来覆盖此行为。转储,但默认情况下行为不匹配。
>>> import collections, simplejson, json
>>> TupleClass = collections.namedtuple("TupleClass", ("a", "b"))
>>> value = TupleClass(1, 2)
>>> json.dumps(value)
'[1, 2]'
>>> simplejson.dumps(value)
'{"a": 1, "b": 2}'
>>> simplejson.dumps(value, namedtuple_as_object=False)
'[1, 2]'
其他回答
我在寻找为Python 2.6安装simplejson时遇到了这个问题。我需要使用json.load()的'object_pairs_hook',以便将json文件加载为OrderedDict。由于熟悉Python的最新版本,我没有意识到Python 2.6的json模块不包括'object_pairs_hook',所以我不得不为此安装simplejson。从个人经验来看,这就是为什么我使用simplejson而不是标准json模块。
在最新版本中,Json在加载和转储两种情况下都比simplejson快
测试版本:
python: 3.6.8 json: 2.0.9 simplejson: 3.16.0
结果:
>>> def test(obj, call, data, times):
... s = datetime.now()
... print("calling: ", call, " in ", obj, " ", times, " times")
... for _ in range(times):
... r = getattr(obj, call)(data)
... e = datetime.now()
... print("total time: ", str(e-s))
... return r
>>> test(json, "dumps", data, 10000)
calling: dumps in <module 'json' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\json\\__init__.py'> 10000 times
total time: 0:00:00.054857
>>> test(simplejson, "dumps", data, 10000)
calling: dumps in <module 'simplejson' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\simplejson\\__init__.py'> 10000 times
total time: 0:00:00.419895
'{"1": 100, "2": "acs", "3.5": 3.5567, "d": [1, "23"], "e": {"a": "A"}}'
>>> test(json, "loads", strdata, 1000)
calling: loads in <module 'json' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\json\\__init__.py'> 1000 times
total time: 0:00:00.004985
{'1': 100, '2': 'acs', '3.5': 3.5567, 'd': [1, '23'], 'e': {'a': 'A'}}
>>> test(simplejson, "loads", strdata, 1000)
calling: loads in <module 'simplejson' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\simplejson\\__init__.py'> 1000 times
total time: 0:00:00.040890
{'1': 100, '2': 'acs', '3.5': 3.5567, 'd': [1, '23'], 'e': {'a': 'A'}}
版本:
python: 3.7.4 json: 2.0.9 simplejson: 3.17.0
Json在转储操作中比simplejson快,但在加载操作中两者保持相同的速度
内置json模块被包含在Python 2.6中。任何支持Python < 2.6版本的项目都需要有一个备份。在很多情况下,这种退路很简单。
Json是simplejson,添加到stdlib中。但由于json是在2.6中添加的,因此simplejson具有在更多Python版本(2.4+)上工作的优势。
simplejson的更新频率也高于Python,因此如果您需要(或想要)最新版本,最好尽可能使用simplejson本身。
在我看来,一个好的做法是使用其中一种作为后备。
try:
import simplejson as json
except ImportError:
import json
下面是Python json库的比较(现在已经过时了):
比较Python的JSON模块(存档链接)
不管这个比较的结果如何,如果你使用的是Python 2.6,你应该使用标准库json。和. .否则还不如直接使用simplejson。
推荐文章
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 如何合并2 JSON对象从2个文件使用jq?
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- Python日志记录不输出任何东西
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 美丽的汤和提取div及其内容的ID