如何在Python中将对象列表转换为json?

时间:2019-05-01 17:21:37

标签: python json python-3.x

我有一个名为Employee的类,并且有包含Employee实例的列表。我尝试了以下方法:

class Employee:
    empId
    empName

    def __init__(self, empId, empName):
            self.empId = empId
            self.empName = empName

    class Test:
        def convertJson(self):
            employees=[]
            emp1 = Employee(101,'John')
            employees.append(emp1)
            emp2 = Employee(102,'Smith')
            employees.append(emp2)
            employeeList = json.dumps(employees.__dict__) # Need to convert employees into json

它引发异常,列表没有属性'dict

0 个答案:

没有答案