自定义类JSON很好

时间:2019-02-04 12:46:08

标签: python python-3.x

我有一个自定义课程

asctime

当我尝试将其响应转换为json对象时,出现错误:class ApiResponse(object): # HTTP status code status_code = 200 # HTTP exception type type_response = "ApiResponse" # response data data = None def __init__(self, uuid_event): """ constructor """ import json response = { "type": self.type_response, "statusCode": str(self.status_code), "message": self.message, "correlationId": uuid_event } if self.data or isinstance(self.data, list): response['data'] = self.data # message format self.message = json.dumps(response) def __repr__(self): """ representation of object """ return repr(self.message) def read(self): """ read method allow json.load() """ return self.message

我需要将其作为可序列化的json返回

这是实例的结果:

TypeError: Object of type OkResponse is not JSON serializable

是否有任何魔术方法可以使此类json可序列化?

0 个答案:

没有答案