【python】 Json 对象映射

import json
import functools

# json->object 映射函数(不支持函数方法成员)
def __praseObject(jsonStr, Class):
    """Class needs a constructor with no arguments"""

    data = json.loads(jsonStr)
    result = Class()
    result.__dict__ = data
    return result

# 绑定函数
json.fromObject = functools.partial(json.dumps, default=lambda obj: obj.__dict__)
json.toObject = __praseObject


# Example code
class Student(object):
    def __init__(self, name="", age=0):
        super().__init__()
        self.name = name
        self.age = age

student = Student("wavky", 18)

jsonStr = json.fromObject(student)  # student -> json
newStudent = json.toObject(jsonStr, Student)  # json -> student

你可能感兴趣的:(【python】 Json 对象映射)