亚洲一区精品自拍_2021年国内精品久久_男同十八禁gv在线观看_免费观看a级性爱黄片

Article / 文章中心

Python編程:json序列化python對象

發(fā)布時(shí)間:2021-11-23 點(diǎn)擊數(shù):740

相關(guān): Python編程:json模塊和pickle模塊

# -*- coding: utf-8 -*-  # @File    : json_demo.py # @Date    : 2018-06-04 # @Author  : Peng Shiyu  # 定制序列化或反序列化的規(guī)則 import json  class Student(object):  def __init__(self, name, age):  self.name = name  self.age = age   if __name__ == '__main__':  s = Student("Tom", 23)   # 序列化  ret = json.dumps(s, default=lambda obj: obj.__dict__)  print(ret)  # {"name": "Tom", "age": 23}   # 反序列化  def obj2student(d):  return Student(d["name"], d["age"])   s2 = json.loads(ret, object_hook=obj2student)  print(s2)  # <__main__.Student object at 0x101c7e518>