我想将一个相当复杂的json字符串转换为swift结构,但api使用了一堆包装器键,但我不确定该如何快速构建:
JSON
{
"title": "this is a title",
"children": {
"attachment": {
"results": [
{
"id": "att241845336",
"metadata": {
"mediaType": "application/vnd.ms-excel.sheet.macroEnabled.12"
},
"extensions": {
"fileSize": 430395,
}
},
{
"id": "att241844542",
"metadata": {
"mediaType": "image/png"
},
"extensions": {
"fileSize": 114781,
}
}
],
"size": 25,
},
"_links": {
"self": "https://example.com/rest/api/content/241845389/child"
},
"_expandable": {
"comment": "/rest/api/content/241845389/child/comment",
"page": "/rest/api/content/241845389/child/page"
}
},
"_links": {
"collection": "/rest/api/content",
}
}
我尝试过类似的方法,但我想我必须想出一些方法来避免在结构中两次出现“链接”之类的变量……我也不确定我是否正确进行了包装:
swift4
struct MetaMessage : Codable {
struct Message : Codable {
let title: String
let children: [child]
let _links: [link]
}
struct child {
let attachment: [attachments]
let _links: [link]
let _expandable: [expandables]
}
struct attachments {
let results: [result]
let size: int
}
struct result {
let id: String
let metadata: [metadataSub]
let extensions: [extension]
}
struct metadataSub {
let mediaType: Sting
}
struct extension {
let fileSize: Long
}
struct link {
let self: URL
}
struct expandables {
let comment: String
let page: String
}
struct expandables {
let collection: String
}
}