Python的JSON对象映射器

2024-10-01 02:23:41 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我正在用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”


Tags: from模型importapidatetimetimelibclass
1条回答
网友
1楼 · 发布于 2024-10-01 02:23:41

以下代码有效:

需要改进的几点:

  • 使用类型挂钩转换数据类型

  • 处理camelCase and snake_case

      from dataclasses import dataclass
      from datetime import datetime
      from uuid import UUID
      from typing import List
      from dacite import from_dict
    
    
      @dataclass
      class BusinessUnit:
          code: int
          type: str
    
    
      @dataclass
      class TimeWindow:
          free_capacity: bool
          capacity_template_identifier: str
          cut_off: str
          end: str
          max_capacity: int
          start: str
          time_window_identifier: str
          tsp_id: int
    
    
      @dataclass
      class WelcomeElement:
          business_unit: BusinessUnit
          time_windows: List[TimeWindow]
          time_zone: str
    
      data = [{
          "business_unit": {
              "code": 27888,
              "type": "LSCCC"
          },
          "time_windows": [
              {
                  "free_capacity": True,
                  "capacity_template_identifier": "xxxxxxxxxxxx",
                  "cut_off": "2020-11-30T17:00:00Z",
                  "end": "2020-11-30T17:00:00Z",
                  "max_capacity": 0,
                  "start": "2020-11-30T08:00:00Z",
                  "time_window_identifier": "yyyyyyyyyyyyyy",
                  "tsp_id": 9900001
              }
          ],
          "time_zone": "Europe/London"
      }]
    
      print('start')
      result = from_dict(data_class=WelcomeElement, data=data[0])
      print(result)
    

    输出

      start
      WelcomeElement(business_unit=BusinessUnit(code=27888, type='LSCCC'), time_windows=[TimeWindow(free_capacity=True, capacity_template_identifier='xxxxxxxxxxxx', cut_off='2020-11-30T17:00:00Z', end='2020-11-30T17:00:00Z', max_capacity=0, start='2020-11-30T08:00:00Z', time_window_identifier='yyyyyyyyyyyyyy', tsp_id=9900001)], time_zone='Europe/London')
    

相关问题 更多 >