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 
15
#JSONEncoder().encode(res)json.dumps(res,ensure_ascii=False)相同
  1. 使用方法
JSONEncoder().encode(res)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2021-06-20
  • 2022-12-23
  • 2021-11-07
猜你喜欢
  • 2022-12-23
  • 2021-09-04
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案