区别是,

object_pairs_hook 的传入参数是有序的键值对表,而 object_hook 是无序的dict。并且两个参数都给的话,object_pairs_hook 的优先级要更高。

看一下官方3.6文档:

object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders.If object_hook is also defined, the object_pairs_hook takes priority.

翻译一下:

object_hook是一个可选的函数,它将被任何对象字面值解码(dict)的结果调用。将使用object_hook的返回值而不是dict。这个功能可以用于实现自定义的解码器(例如,JSON-RPC类提示)。

object_pairs_hook是一个可选的函数,它将使用任何对象字面值的结果进行调用,并使用对的有序列表进行解码。将使用object_pairs_hook的返回值,而不是dict。该特征可以用于实现依赖于键和值对被解码的顺序的自定义解码器(例如,collections.OrderedDict()将记住插入的顺序)。如果还定义了object_hook,则object_pairs_hook优先。

在版本3.1中已更改:添加对object_pairs_hook的支持。

 

不同就一个:

with an ordered list of pairs.

就是给object_pairs_hook的解码的键值对列表是有序的,所以一些依赖键值对顺序的功能可以用 object_pairs_hook 参数,而不能用 object_hook 

例子:

>>> j='{"a":1,"b":2,"c":3}'
>>> from json import loads
>>> loads(j,object_hook=lambda x:print(type(x),x))
<class 'dict'> {'b': 2, 'a': 1, 'c': 3}
>>> loads(j,object_pairs_hook=lambda x:print(type(x),x))
<class 'list'> [('a', 1), ('b', 2), ('c', 3)]
>>> from collections import OrderedDict
>>> json.loads(j, object_pairs_hook=OrderedDict)
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

最后推荐个网站叫“一译”,有好多外语文档的翻译。但全都是网友翻译的,所以质量比较捉急。好处是可以句句对应中英对照一起看,并且参与翻译门槛很低。我一般看官网手册看不懂的时候就到这个网站对应目录下找。

https://yiyibooks.cn/xx/python_352/library/json.html