考虑这个名为h.json
的json文件,我想将它转换为python。
{
"acc1":{
"email":"acc1@example.com",
"password":"acc1",
"name":"ACC1",
"salary":1
},
"acc2":{
"email":"acc2@example.com",
"password":"acc2",
"name":"ACC2",
"salary":2
}
}
我可以使用另一个构造函数来获取每个帐户,例如:
import json
from dataclasses import dataclass
@dataclass
class Account(object):
email:str
password:str
name:str
salary:int
@classmethod
def from_json(cls, json_key):
file = json.load(open("h.json"))
return cls(**file[json_key])
但这仅限于哪些论点(电子邮件、姓名等)。都是在数据集中定义的。
如果我修改json以包括另一件事,比如年龄,该怎么办?脚本将返回一个TypeError
,特别是TypeError: __init__() got an unexpected keyword argument 'age'
。
是否有一种方法可以根据dict (json对象)的键动态调整类属性,这样我就不必每次向json添加新键时都要添加属性了?
发布于 2021-10-29 19:11:14
这样,您就失去了一些dataclass
特性。
optional
然而,您更熟悉您的项目并做出相应的决定。
必须有许多方法,但这是其中之一:
@dataclass
class Account(object):
email: str
password: str
name: str
salary: int
@classmethod
def from_json(cls, json_key):
file = json.load(open("1.txt"))
keys = [f.name for f in fields(cls)]
# or: keys = cls.__dataclass_fields__.keys()
json_data = file[json_key]
normal_json_data = {key: json_data[key] for key in json_data if key in keys}
anormal_json_data = {key: json_data[key] for key in json_data if key not in keys}
tmp = cls(**normal_json_data)
for anormal_key in anormal_json_data:
setattr(tmp,anormal_key,anormal_json_data[anormal_key])
return tmp
test = Account.from_json("acc1")
print(test.age)
发布于 2021-10-29 22:15:58
由于您的数据听起来可能是动态的,并且您希望可以在JSON对象中添加更多字段而不反映模型中的相同更改,所以我还建议检查typing.TypedDict
而不是dataclass
。
下面是TypedDict
的一个示例,它应该在Python中工作。由于TypedDict是在3.8中介绍,所以我从typing_extensions
导入了它,所以它与3.7代码兼容。
from __future__ import annotations
import json
from io import StringIO
from typing_extensions import TypedDict
class Account(TypedDict):
email: str
password: str
name: str
salary: int
json_data = StringIO("""{
"acc1":{
"email":"acc1@example.com",
"password":"acc1",
"name":"ACC1",
"salary":1
},
"acc2":{
"email":"acc2@example.com",
"password":"acc2",
"name":"ACC2",
"salary":2,
"someRandomKey": "string"
}
}
""")
data = json.load(json_data)
name_to_account: dict[str, Account] = data
acct = name_to_account['acc2']
# Your IDE should be able to offer auto-complete suggestions within the
# brackets, when you start typing or press 'Ctrl + Space' for example.
print(acct['someRandomKey'])
如果您打算使用dataclass来建模您的数据,我建议您签出一个JSON序列化库,比如数据处理-向导 (免责声明:我是创建者),它应该处理JSON数据中的无关字段,如果您发现数据变得更加复杂,也应该使用嵌套的数据处理模型。
它还有一个方便的工具,您可以使用它从JSON数据生成数据集模式,例如,如果您想在JSON文件中添加新字段时更新您的模型类,这是非常有用的。
发布于 2021-10-29 18:53:58
对于一个平面(而不是嵌套的数据集),下面的代码完成了这项工作。
如果您需要处理嵌套的数据类型,则应该使用类似于dacite
的框架。
注释1 --从json文件加载数据不应该是类逻辑的一部分。
如果您的json可以包含任何内容-您不能将它映射到一个数据集,那么您应该使用一个。
from dataclasses import dataclass
from typing import List
data = {
"acc1":{
"email":"acc1@example.com",
"password":"acc1",
"name":"ACC1",
"salary":1
},
"acc2":{
"email":"acc2@example.com",
"password":"acc2",
"name":"ACC2",
"salary":2
}
}
@dataclass
class Account:
email:str
password:str
name:str
salary:int
accounts: List[Account] = [Account(**x) for x in data.values()]
print(accounts)
输出
[Account(email='acc1@example.com', password='acc1', name='ACC1', salary=1), Account(email='acc2@example.com', password='acc2', name='ACC2', salary=2)]
https://stackoverflow.com/questions/69773539
复制相似问题