from dataclasses import dataclass,asdict
import json



@dataclass()
class Student:
    id:int
    name:str
    cpa:float
    addr:str

# 序列化
# p = Student(1,"小强",3.6,"河南郑州市")
# p_dic = asdict(p,)
#
# with open("data.json","w",encoding='utf8') as f:
#     f.write(json.dumps(p_dic,ensure_ascii=False))

#反序列化

with open("data.json","r",encoding='utf8') as f:
    json_dic = json.loads(f.read())

stu = Student(**json_dic)
print(stu)


Logo

2万人民币佣金等你来拿,中德社区发起者X.Lab,联合德国优秀企业对接开发项目,领取项目得佣金!!!

更多推荐