【发布时间】:2019-06-02 22:12:30
【问题描述】:
我有包含此类信息的 json 文件
{
"cars" : [{
"model" : " BMW",
"gas" : 100,
},
{
"model" : "LADA",
"gas" : 150
},
{
"model" : "SUZUKI",
"gas" : 70
}]
}
及以下班
class GasCar(Car):
def __init__(self, model=None, gas=None):
super(GasCar, self).__init__()
self.gas = gas
self.model = model
如何创建类对象并将数据从 json 传输到类实例?
我试过了
car = GasCar()
s = json.dumps(car)
s = json.dumps(car.__dict__)
【问题讨论】:
-
你能分享一下你到目前为止的尝试吗
-
我很困惑,你是想将数据从 GasCar() 传输到 JSON,还是从 JSON 传输到 GasCar?
-
从 json 到 GasCar