【发布时间】:2017-06-25 21:02:21
【问题描述】:
我无法从 MongoDB Json 文档中反序列化 ObjectId() 和 datetime()。
我的 MongoDB Json 文档如下:
result = "[(u'profileDetails', {u'basicDetails': {u'dateOfBirth': datetime.datetime(1992, 2, 1, 0, 0), u'customerCode': u'C037799'}, u'xDirLevel': {u'masterCode': 1}}), (u'_id', ObjectId('58872e99321a0c8633291b3f'))]"
我想反序列化 datetime() 和 ObjectId() 函数。
我的 Python 代码如下:
from bson import json_util
def deserialize_mongoResult(mongoResult)
result = json.dumps(mongoResult, default=json_util.default)
print result
当我打印result 时,如下所示:
"[(u'profileDetails', {u'basicDetails': {u'dateOfBirth': datetime.datetime(1992, 2, 1, 0, 0), u'customerCode': u'C037799'}, u'xDirLevel': {u'masterCode': 1}}), (u'_id', ObjectId('58872e99321a0c8633291b3f'))]"
为什么不反序列化对象
【问题讨论】:
-
反序列化 datetime() 和 ObjectId() 函数是什么意思?你的预期输出是什么?
-
我希望 ObjectId() 和 datetime() 函数不应该出现在结果中。我发现json_util 会达到目的,但使用后结果不会改变。我在stackoverflow中发现了同样的问题,我在那里问了同样的问题,他们建议打开新问题same like question in stackoverflow
-
@arthur,我希望 ObjectId() 值应该更改为
{"_id": {"$oid": "4edebd262ae5e93b41000000"}}和 datetime() 函数我不确定,它应该转换为 json 格式 -
好的,所以我假设您有一个来自 pymongo 的文档,并且您希望它是一个 JSON 文件,所以您想要 serialize 它。我说的对吗?
-
是的,你是对的亚瑟
标签: python