请使用此序列化 Python 3.11,目标是
{"name": "One", "quantity": 2, "students":[{"first_name": "Geeky", "last_name": "Guy"},{"first_name": "GFG", "last_name": "ocks"}] }
Run Code Online (Sandbox Code Playgroud)
import json
class Student():
def __init__(self):
pass
class Team():
def __init__(self):
pass
student1 = Student()
student1.first_name='Geeky'
student1.last_name='Guy'
student2 = Student()
student2.first_name='GFG'
student2.last_name='ocks'
team = Team()
team.name='One'
team.quantity=2
team.students=[]
team.students.append(student1)
team.students.append(student2)
# Serialization
json_data = json.dumps(team.__dict__)
print(json_data)
Run Code Online (Sandbox Code Playgroud)
打印下一个错误
Traceback (most recent call last):
File "example.py", line 28, in <module>
json_data = json.dumps(team.__dict__)
File "/opt/bitnami/python/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/opt/bitnami/python/lib/python3.8/json/encoder.py", line 199, in encode …Run Code Online (Sandbox Code Playgroud)