基本上,我正在用python自动化API,API返回JSON,响应如下所示
在java中,我可以使用POJO模型和Gson lib来验证模式,就像fallow-
GetCustomerModel - is my custom POJO class have so 3 inner classes.
GetCustomerModel body = new Gson().fromJson(response.getBody(),GetCustomerModel.class);
Python中是否有任何lib支持做同样的事情,我是说converjson作为自定义Python映射对象
[{
"businessUnit": {
"code": "27888",
"type": "LSCCC"
},
"timeWindows": [
{
"freeCapacity": true,
"capacityTemplateIdentifier": "xxxxxxxxxxxx",
"cutOff": "2020-11-30T17:00:00Z",
"end": "2020-11-30T17:00:00Z",
"maxCapacity": 0,
"start": "2020-11-30T08:00:00Z",
"timeWindowIdentifier": "yyyyyyyyyyyyyy",
"tspId": "9900001"
}
],
"timeZone": "Europe/London"
}]
Python模型-
from dataclasses import dataclass
from datetime import datetime
from uuid import UUID
from typing import List
@dataclass
class BusinessUnit:
code: int
type: str
@dataclass
class TimeWindow:
free_capacity: bool
capacity_template_identifier: str
cut_off: datetime
end: datetime
max_capacity: int
start: datetime
time_window_identifier: UUID
tsp_id: int
@dataclass
class WelcomeElement:
business_unit: BusinessUnit
time_windows: List[TimeWindow]
time_zone: str
我的密码-
result = from_dict(data_class=WelcomeElement, data=response.content)
给出一个错误,说明“元组索引必须是整数或切片,而不是str”
以下代码有效:
需要改进的几点:
使用类型挂钩转换数据类型
处理camelCase and snake_case
输出
相关问题 更多 >
编程相关推荐