解决TypeError: Object of type 'ObjectId' is not JSON serializable

时间:2020-11-23 00:25:33   收藏:0   阅读:210
 1 import json
 2 from bson import ObjectId
 3 class JSONEncoder(json.JSONEncoder):
 4     ‘‘‘
 5     解决TypeError: Object of type ‘ObjectId‘ is not JSON serializable
 6     ‘‘‘
 7     #ensure_ascii解决中文乱码问题,根据自己情况天假
 8     def __init__(self, ensure_ascii=False):
 9         super().__init__(ensure_ascii=False)
10     def default(self, o):
11         if isinstance(o, ObjectId):
12             return str(o)
13         return json.JSONEncoder.default(self, o)
14 #使用,res中有‘ObjectID‘
15 
16 JSONEncoder().encode(res)
17  
18  

 

原文:https://www.cnblogs.com/chuanyang/p/14022211.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!