How to convert JSON data into a Python class?
class AnotherClass():
test: int
class MyClass():
a: int
c: AnotherClass
json = {
"a": 1,
"c": {
"test": 2
}
}
my_class = MyClass(json)
print(my_class.a) # 1
print(my_class.c.test) # error
I've tried using the _dict_ attribute, but the variable in the member class is not assigned a value.
Is there a way to parse the json and input data up to the variable of the member class without manually assigning values?
class MyClass():
a: int
c: AnotherClass
def __init__(self, json) -> None:
self.__dict__ = json
my_class = MyClass(json)
print(my_class.a) # 1
print(my_class.c.test) # AttributeError: 'dict' object has no attribute 'test'