无法使用id键转储python字典

无法使用id键转储python字典,python,json,dump,Python,Json,Dump,有什么问题吗?尝试使用标准json库,并将其转储 有了这个改变,我觉得很好 json.dump({'id' : '3'}) File "/Library/Python/2.7/site-packages/simplejson/__init__.py", line 354, in dumps return _default_encoder.encode(obj) File "/Library/Python/2.7/site-packages/simplejson/encoder.

有什么问题吗?

尝试使用标准json库,并将其转储

有了这个改变,我觉得很好

json.dump({'id' : '3'})

  File "/Library/Python/2.7/site-packages/simplejson/__init__.py", line 354, in dumps
    return _default_encoder.encode(obj)
  File "/Library/Python/2.7/site-packages/simplejson/encoder.py", line 262, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Python/2.7/site-packages/simplejson/encoder.py", line 340, in iterencode
    return _iterencode(o, 0)
  File "/Library/Python/2.7/site-packages/simplejson/encoder.py", line 239, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ObjectId('520183b519744a0ca3d36003') is not JSON serializable

您真的应该说明您使用的是simplejson库,而不是默认的json模块。我假设您有类似于将simplejson导入为json的东西?如果其他人要看这段代码,这是一个值得怀疑的决定,因为没有人希望
json
引用
simplejson
。(使用simplejson没有什么问题,只是不要将其作为json导入。)

代码中的错误不会由顶部的示例引发,例如:

>>> import json
>>> json.dumps({'id' : '3'})
'{"id": "3"}'
>将simplejson导入为json
>>>json.dump({'id':'3'})
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:dump()至少接受2个参数(给定1个)
您在问题中显示的错误可能是因为您试图从未序列化的对象创建JSON数据结构。我们需要查看您的对象以了解真正失败的地方,并提供更好的解决方案

例如:

>>> import simplejson as json
>>> json.dump({ 'id' : '3' })
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dump() takes at least 2 arguments (1 given)
>>类demoObj:
...   定义初始化(自):
...     self.a='1'
...
>>>testObj=demoObj()
>>>json.dumps(testObj)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python2.7/json/_init_uuu.py”,第231行,转储
返回默认编码器编码(obj)
文件“/usr/lib/python2.7/json/encoder.py”,第201行,在encode中
chunks=self.iterencode(o,\u one\u shot=True)
iterencode中的文件“/usr/lib/python2.7/json/encoder.py”,第264行
返回_iterencode(o,0)
默认情况下,文件“/usr/lib/python2.7/json/encoder.py”第178行
raise TypeError(repr(o)+“不可JSON序列化”)
TypeError:不可序列化JSON
请注意,此处的错误与您的错误相同(对对象的引用除外)

为了序列化我的testObj,我基本上需要能够从中获取字典。在这些问题中可以看到一些这样做的参考:


转储
istead of
转储
。您使用
simplejson
而不是标准库
json
模块还有什么特别的原因吗?您可以添加您试图序列化的实际对象,以便我们了解它抛出您显示的错误的原因吗?您给出的示例与导致错误的原因不同。可能是因为
将某个名称导入为name
是一种常见的做法。示例:
从xml.etree导入元素树作为etree
从lxml导入etree
。这样,用户就可以在它们之间进行交换,而无需更改每个用法。
>>> class demoObj:
...   def __init__(self):
...     self.a = '1'
...
>>> testObj = demoObj()
>>> json.dumps(testObj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <__main__.demoObj instance at 0xe5b7a0> is not JSON serializable