class UpdateParams:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __getattr__(self, item):
        print(f"没有该属性:{item}")
        return None


def json2obj(json_data):
    d = UpdateParams.__new__(UpdateParams)
    d.__dict__.update(json_data)
    return d


if __name__ == '__main__':
    j = {'name': "zhangsan", 'age': 28, 'gender': '男', 'score': {"english": 90, "math": 80, "history": 65},
         'like': ["tv", "dvd", "phone"]}
    obj = json2obj(j)
    print(obj.name)
    print(obj.age)
    print(obj.gender)
    print(obj.score)
    score = json2obj(obj.score)
    print(score.english)
    print(obj.like)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-06-01
  • 2022-01-01
相关资源
相似解决方案