python 自定义对象序列化为json

1、自定义对象

class Evaluation:
    def __init__(self, train_f1, train_iou, train_loss, valid_f1, valid_iou, valid_loss):
        self.trainF1 = train_f1
        self.trainIou = train_iou
        self.trainLoss = train_loss
        self.validF1 = valid_f1
        self.validIou = valid_iou
        self.validLoss = valid_loss

2、实例化

evaluation = Evaluation(train_f1_list, train_iou_list, train_loss_list, valid_f1_list, valid_iou_list,
                            valid_loss_list)

3、序列化

import json

evaluation_json = json.dumps(evaluation, ensure_ascii=False, default=lambda obj: obj.__dict__)

你可能感兴趣的:(python,json,深度学习)