问题:我可以将JSON加载到OrderedDict吗?
好的,所以我可以在中使用OrderedDict json.dump
。也就是说,OrderedDict可以用作JSON的输入。
但是可以用作输出吗?如果可以,怎么办?就我而言,我想load
放入OrderedDict,以便可以将键的顺序保留在文件中。
如果没有,是否有某种解决方法?
Ok so I can use an OrderedDict in json.dump
. That is, an OrderedDict can be used as an input to JSON.
But can it be used as an output? If so how? In my case I’d like to load
into an OrderedDict so I can keep the order of the keys in the file.
If not, is there some kind of workaround?
回答 0
是的你可以。通过指定JSONDecoder的object_pairs_hook
参数。实际上,这是文档中给出的确切示例。
>>> json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode('{"foo":1, "bar": 2}')
OrderedDict([('foo', 1), ('bar', 2)])
>>>
您可以将此参数传递给json.loads
(如果不需要出于其他目的的Decoder实例),如下所示:
>>> import json
>>> from collections import OrderedDict
>>> data = json.loads('{"foo":1, "bar": 2}', object_pairs_hook=OrderedDict)
>>> print json.dumps(data, indent=4)
{
"foo": 1,
"bar": 2
}
>>>
使用json.load
以相同的方式完成:
>>> data = json.load(open('config.json'), object_pairs_hook=OrderedDict)
Yes, you can. By specifying the object_pairs_hook
argument to JSONDecoder. In fact, this is the exact example given in the documentation.
>>> json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode('{"foo":1, "bar": 2}')
OrderedDict([('foo', 1), ('bar', 2)])
>>>
You can pass this parameter to json.loads
(if you don’t need a Decoder instance for other purposes) like so:
>>> import json
>>> from collections import OrderedDict
>>> data = json.loads('{"foo":1, "bar": 2}', object_pairs_hook=OrderedDict)
>>> print json.dumps(data, indent=4)
{
"foo": 1,
"bar": 2
}
>>>
Using json.load
is done in the same way:
>>> data = json.load(open('config.json'), object_pairs_hook=OrderedDict)
回答 1
适用于Python 2.7+的简单版本
my_ordered_dict = json.loads(json_str, object_pairs_hook=collections.OrderedDict)
或适用于Python 2.4至2.6
import simplejson as json
import ordereddict
my_ordered_dict = json.loads(json_str, object_pairs_hook=ordereddict.OrderedDict)
Simple version for Python 2.7+
my_ordered_dict = json.loads(json_str, object_pairs_hook=collections.OrderedDict)
Or for Python 2.4 to 2.6
import simplejson as json
import ordereddict
my_ordered_dict = json.loads(json_str, object_pairs_hook=ordereddict.OrderedDict)
回答 2
一些好消息!从3.6版开始,cPython实现保留了字典的插入顺序(https://mail.python.org/pipermail/python-dev/2016-September/146327.html)。这意味着json库现在默认保留顺序。观察python 3.5和3.6之间的行为差异。编码:
import json
data = json.loads('{"foo":1, "bar":2, "fiddle":{"bar":2, "foo":1}}')
print(json.dumps(data, indent=4))
在py3.5中,结果顺序是不确定的:
{
"fiddle": {
"bar": 2,
"foo": 1
},
"bar": 2,
"foo": 1
}
在python 3.6的cPython实现中:
{
"foo": 1,
"bar": 2,
"fiddle": {
"bar": 2,
"foo": 1
}
}
真正的好消息是,这已成为python 3.7的语言规范(与cPython 3.6+的实现细节相反):https ://mail.python.org/pipermail/python-dev/2017-December/151283 .html
因此,您的问题的答案现在变成:升级到python 3.6!:)
Some great news! Since version 3.6 the cPython implementation has preserved the insertion order of dictionaries (https://mail.python.org/pipermail/python-dev/2016-September/146327.html). This means that the json library is now order preserving by default. Observe the difference in behaviour between python 3.5 and 3.6. The code:
import json
data = json.loads('{"foo":1, "bar":2, "fiddle":{"bar":2, "foo":1}}')
print(json.dumps(data, indent=4))
In py3.5 the resulting order is undefined:
{
"fiddle": {
"bar": 2,
"foo": 1
},
"bar": 2,
"foo": 1
}
In the cPython implementation of python 3.6:
{
"foo": 1,
"bar": 2,
"fiddle": {
"bar": 2,
"foo": 1
}
}
The really great news is that this has become a language specification as of python 3.7 (as opposed to an implementation detail of cPython 3.6+): https://mail.python.org/pipermail/python-dev/2017-December/151283.html
So the answer to your question now becomes: upgrade to python 3.6! :)
回答 3
除了转储字典,您总是可以写出密钥列表,然后OrderedDict
通过遍历列表来重建密钥?
You could always write out the list of keys in addition to dumping the dict, and then reconstruct the OrderedDict
by iterating through the list?
回答 4
除了在字典旁边转储键的有序列表之外,另一种具有显式优点的低技术解决方案是转储键-值对的(有序)列表ordered_dict.items()
。加载很简单OrderedDict(<list of key-value pairs>)
。尽管JSON没有这个概念(JSON字典没有顺序),但这仍然可以处理有序字典。
利用json
以正确顺序转储OrderedDict 的事实确实很好。但是,通常必须将所有 JSON字典作为OrderedDict 读取(通过object_pairs_hook
参数)是不必要的繁琐操作,也不一定有意义,因此仅显式转换必须排序的字典也是有意义的。
In addition to dumping the ordered list of keys alongside the dictionary, another low-tech solution, which has the advantage of being explicit, is to dump the (ordered) list of key-value pairs ordered_dict.items()
; loading is a simple OrderedDict(<list of key-value pairs>)
. This handles an ordered dictionary despite the fact that JSON does not have this concept (JSON dictionaries have no order).
It is indeed nice to take advantage of the fact that json
dumps the OrderedDict in the correct order. However, it is in general unnecessarily heavy and not necessarily meaningful to have to read all JSON dictionaries as an OrderedDict (through the object_pairs_hook
argument), so an explicit conversion of only the dictionaries that must be ordered makes sense too.
回答 5
如果指定object_pairs_hook参数,则通常使用的load命令将起作用:
import json
from collections import OrderedDict
with open('foo.json', 'r') as fp:
metrics_types = json.load(fp, object_pairs_hook=OrderedDict)
The normally used load command will work if you specify the object_pairs_hook parameter:
import json
from collections import OrderedDict
with open('foo.json', 'r') as fp:
metrics_types = json.load(fp, object_pairs_hook=OrderedDict)