如何将 DictProxy 对象转换为 JSON 可序列化字典?
How to convert a DictProxy object into JSON serializable dict?
我有一个 DictProxy
使用 multiprocessing.Manager().dict()
创建的对象来支持并发。在 运行 的末尾,我需要将字典序列化为 JSON。但不清楚如何将 DictProxy
转换为可序列化的字典对象。当我尝试时,我得到:
TypeError: <DictProxy object, typeid 'dict' at 0x10a240ed0> is not JSON serializable
我该如何解决这个问题?
使用 dict_proxy._getvalue()
获取代理下的实际 dict
实例,并将其传递给 json.dump
(或您正在使用的任何方法)。
>>> import multiprocessing
>>> m = multiprocessing.Manager()
>>> d = m.dict()
>>> import json
>>> json.dumps(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib64/python2.6/json/encoder.py", line 317, in _iterencode
for chunk in self._iterencode_default(o, markers):
File "/usr/lib64/python2.6/json/encoder.py", line 323, in _iterencode_default
newobj = self.default(o)
File "/usr/lib64/python2.6/json/encoder.py", line 344, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <DictProxy object, typeid 'dict' at 0x97eed0> is not JSON serializable
>>> json.dumps(d._getvalue())
'{}'
与其使用像 _getvalue()
这样的私有 DictProxy
方法,我更喜欢使用像 copy()
这样的 public 方法,其中 returns 是浅层的-已复制 dict
.
import multiprocessing
if __name__ == '__main__':
manager = multiprocessing.Manager()
d = manager.dict()
import json
json.dumps(d.copy())
迟到的答案,但我解决了以下错误:
TypeError: Object of type 'DictProxy' is not JSON serializable
TypeError: Object of type 'ListProxy' is not JSON serializable
使用:
from multiprocessing import Manager
manager = Manager()
# For Dicts
x = manager.dict()
json.dumps(dict(x))
# For Lists
x = manager.list()
json.dumps(list(x))
我有一个 DictProxy
使用 multiprocessing.Manager().dict()
创建的对象来支持并发。在 运行 的末尾,我需要将字典序列化为 JSON。但不清楚如何将 DictProxy
转换为可序列化的字典对象。当我尝试时,我得到:
TypeError: <DictProxy object, typeid 'dict' at 0x10a240ed0> is not JSON serializable
我该如何解决这个问题?
使用 dict_proxy._getvalue()
获取代理下的实际 dict
实例,并将其传递给 json.dump
(或您正在使用的任何方法)。
>>> import multiprocessing
>>> m = multiprocessing.Manager()
>>> d = m.dict()
>>> import json
>>> json.dumps(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib64/python2.6/json/encoder.py", line 317, in _iterencode
for chunk in self._iterencode_default(o, markers):
File "/usr/lib64/python2.6/json/encoder.py", line 323, in _iterencode_default
newobj = self.default(o)
File "/usr/lib64/python2.6/json/encoder.py", line 344, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <DictProxy object, typeid 'dict' at 0x97eed0> is not JSON serializable
>>> json.dumps(d._getvalue())
'{}'
与其使用像 _getvalue()
这样的私有 DictProxy
方法,我更喜欢使用像 copy()
这样的 public 方法,其中 returns 是浅层的-已复制 dict
.
import multiprocessing
if __name__ == '__main__':
manager = multiprocessing.Manager()
d = manager.dict()
import json
json.dumps(d.copy())
迟到的答案,但我解决了以下错误:
TypeError: Object of type 'DictProxy' is not JSON serializable
TypeError: Object of type 'ListProxy' is not JSON serializable
使用:
from multiprocessing import Manager
manager = Manager()
# For Dicts
x = manager.dict()
json.dumps(dict(x))
# For Lists
x = manager.list()
json.dumps(list(x))