我有一个深度嵌套的 JSON 结构,如下所示:
json_data = """{
"title: "...",
"links": [
{
"href": "string",
"method": {
"method": "string"
},
"rel": "string"
}
]
}"""
我的课程:
import attr
import typing as ty
import enum
class HttpMethod(enum.Enum):
GET = 0
HEAD = 1
POST = 2
PUT = 3
DELETE = 4
CONNECT = 5
OPTIONS = 6
TRACE = 7
PATCH = 8
@attr.s(frozen=True, auto_attribs=True)
class LinkInfo:
href: str = attr.ib()
link: str = attr.ib(converter=ensure_cls)
method: HttpMethod = attr.ib(converter=lambda x: x.name)
@attr.s(frozen=True, auto_attribs=True)
class AnalysisTemplateGetModel:
title: x: str = attr.ib()
links: ty.List[mlinks.LinkInfo]= attr.ib(default=attr.Factory(list))
我想将它们反序列化为 attr 类,
js = json.loads(**json_data)
但是js的links字段还是字典,不是LinkInfo对象?
str(js)
AnalysisTemplateGetModel(..., links=[{'href': 'string', 'method': {'method': 'string'}, 'rel': 'string'}])