[英]Python to JSON Serialization
I'm using the following code to serialize my Python code to JSON: 我正在使用以下代码将我的Python代码序列化为JSON:
def toJson(self):
return json.dumps(self, default=lambda obj: obj.__dict__, indent=4)
However, I'm trying to sort out the elements in obj.__dict__
that equal None
. 但是,我正在尝试整理
obj.__dict__
中等于None
的元素。 How would I go about doing this? 我将如何去做呢?
new_dict = dict([(attr,self.__dict__[attr]) for attr in self.__dict__ if self.__dict__[attr]])
return json.dumps(new_dict, default=lambda obj: obj.__dict__, indent=4)
should do it 应该做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.