我正在学习Swift 4的一些新内容,并且正在尝试在API和jsonDecode上发短信。
从我一直在阅读和观看的内容中,添加了我的课程,但是我遇到了一个问题,该问题似乎很难找到答案,并且是带有名称的嵌套json。
下面是我的课(故事):
import Foundation
class Story: Codable{
let item: Item
}
struct Item: Codable{
let featured: Int
let id: Int
struct elementText: Codable{
let id: Int
let name: String
let text: String
}
let element_texts: [elementText]
}
这是json端点返回的内容:
[
-{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/items\/1,
"public" : true,
"featured" : true,
"added" : 2018-07-12T06:31:33+00:00,
"modified" : 2018-07-12T14:00:51+00:00,
"item_type" : -{
"id" : 18,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/item_types\/18,
"name" : Curatescape Story,
"resource" : item_types
},
"collection" : ,
"owner" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/users\/1,
"resource" : users
},
"files" : -{
"count" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/files?item=1,
"resource" : files
},
"tags" : -[
-{
"id" : 2,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/tags\/2,
"name" : Research,
"resource" : tags
},
-{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/tags\/1,
"name" : Review,
"resource" : tags
}
],
"element_texts" : -[
-{
"html" : false,
"text" : Woehler and Force Farm Equipment Building,
"element_set" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/1,
"name" : Dublin Core,
"resource" : element_sets
},
"element" : -{
"id" : 50,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/50,
"name" : Title,
"resource" : elements
}
},
-{
"html" : false,
"text" : Woehler and Force Farm Equipment,
"element_set" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/1,
"name" : Dublin Core,
"resource" : element_sets
},
"element" : -{
"id" : 39,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/39,
"name" : Creator,
"resource" : elements
}
},
-{
"html" : false,
"text" : Street view of the front exterior of Woehler and Force Farm Equipment. Several automobiles are visible through the windows of the store. The alley along the side of the building is also visible. There are several signs along the front of the building reading, 'Farm Equipment,' 'Kaiser Frazer W&F,' and 'Woehler & Force.'; Verso There is a sticker with typed black ink reading, 'Woehler & Force 1316-22 8th Ave. - Greeley, CO 1947. Orig. env. says Liberty Trucker Parts Co.\/ 690 Lincoln St.\/F.V. Altwater\/POB 1889\/Denver, Colo.',
"element_set" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/3,
"name" : Item Type Metadata,
"resource" : element_sets
},
"element" : -{
"id" : 54,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/54,
"name" : Story,
"resource" : elements
}
}
],
"extended_resources" : -{
"exhibit_pages" : -{
"count" : 0,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/exhibit_pages?item=1,
"resource" : exhibit_pages
},
"geolocations" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/geolocations\/1,
"resource" : geolocations
}
}
},
-{
"id" : 2,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/items\/2,
"public" : true,
"featured" : false,
"added" : 2018-07-12T13:47:27+00:00,
"modified" : 2018-07-12T14:00:51+00:00,
"item_type" : -{
"id" : 18,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/item_types\/18,
"name" : Curatescape Story,
"resource" : item_types
},
"collection" : ,
"owner" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/users\/1,
"resource" : users
},
"files" : -{
"count" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/files?item=2,
"resource" : files
},
"tags" : -[
-{
"id" : 2,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/tags\/2,
"name" : Research,
"resource" : tags
},
-{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/tags\/1,
"name" : Review,
"resource" : tags
}
],
"element_texts" : -[
-{
"html" : false,
"text" : 703 11th Avenue,
"element_set" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/1,
"name" : Dublin Core,
"resource" : element_sets
},
"element" : -{
"id" : 50,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/50,
"name" : Title,
"resource" : elements
}
},
-{
"html" : false,
"text" : Exterior of the front and side of Cloverleaf Dairy. There is a sign above the door that reads, 'Cloverleaf Dairy Ice Cream.' A few of the windows have advertisements on them. A sign near the back of the building reads, 'Cream Receiving Door Butter Fat 32.' A portion of the building behind the dairy is also visible. ; Verso There is a sticker with typed black ink that reads, 'Cloverleaf Dairy - Exterior 703 11th Ave. - Greeley, CO Aug., 1936. Dale Miller & Harold Helton proprietors.',
"element_set" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/3,
"name" : Item Type Metadata,
"resource" : element_sets
},
"element" : -{
"id" : 54,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/54,
"name" : Story,
"resource" : elements
}
}
],
"extended_resources" : -{
"exhibit_pages" : -{
"count" : 0,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/exhibit_pages?item=2,
"resource" : exhibit_pages
},
"geolocations" : -{
"id" : 2,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/geolocations\/2,
"resource" : geolocations
}
}
},
-{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/items\/3,
"public" : true,
"featured" : false,
"added" : 2018-07-13T15:59:09+00:00,
"modified" : 2018-07-13T15:59:09+00:00,
"item_type" : -{
"id" : 18,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/item_types\/18,
"name" : Curatescape Story,
"resource" : item_types
},
"collection" : ,
"owner" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/users\/1,
"resource" : users
},
"files" : -{
"count" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/files?item=3,
"resource" : files
},
"tags" : -[
],
"element_texts" : -[
-{
"html" : false,
"text" : 514 7th Street,
"element_set" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/1,
"name" : Dublin Core,
"resource" : element_sets
},
"element" : -{
"id" : 50,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/50,
"name" : Title,
"resource" : elements
}
},
-{
"html" : false,
"text" : Exterior view of Capron Truck Lines. There are five trucks parked in front of the building along with five men standing by the trucks. There is a sign on the front of the building that reads, 'Capron Truck Lines City Transfer Moving & Storage Phone 2341 Day or Night.'; Verso There is a sticker with typed black ink that reads, 'Capron Truck Lines 514 7th St. - Greeley, CO 7-15-1939. C.H. Capron was the owner.',
"element_set" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/3,
"name" : Item Type Metadata,
"resource" : element_sets
},
"element" : -{
"id" : 54,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/54,
"name" : Story,
"resource" : elements
}
},
-{
"html" : false,
"text" : 514 7th St.,
"element_set" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/3,
"name" : Item Type Metadata,
"resource" : element_sets
},
"element" : -{
"id" : 59,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/59,
"name" : Street Address,
"resource" : elements
}
}
],
"extended_resources" : -{
"exhibit_pages" : -{
"count" : 0,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/exhibit_pages?item=3,
"resource" : exhibit_pages
},
"geolocations" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/geolocations\/3,
"resource" : geolocations
}
}
},
-{
"id" : 4,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/items\/4,
"public" : true,
"featured" : false,
"added" : 2018-07-13T16:18:52+00:00,
"modified" : 2018-07-13T16:18:52+00:00,
"item_type" : -{
"id" : 18,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/item_types\/18,
"name" : Curatescape Story,
"resource" : item_types
},
"collection" : ,
"owner" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/users\/1,
"resource" : users
},
"files" : -{
"count" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/files?item=4,
"resource" : files
},
"tags" : -[
],
"element_texts" : -[
-{
"html" : false,
"text" : 1903 11th Street,
"element_set" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/1,
"name" : Dublin Core,
"resource" : element_sets
},
"element" : -{
"id" : 50,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/50,
"name" : Title,
"resource" : elements
}
},
-{
"html" : false,
"text" : Street view of the front and side of 1903 11th Street. Most of the front porch is obscured by a shrub but the house number at the top of the porch is visible. The sidewalk leading up to the house and around the house is also visible. A portion of the house behind the property is also visible. ; Verso Written in black ink, 'Home at 1903 11th Street Greeley, CO.',
"element_set" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/3,
"name" : Item Type Metadata,
"resource" : element_sets
},
"element" : -{
"id" : 54,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/54,
"name" : Story,
"resource" : elements
}
}
],
"extended_resources" : -{
"exhibit_pages" : -{
"count" : 0,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/exhibit_pages?item=4,
"resource" : exhibit_pages
},
"geolocations" : -{
"id" : 4,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/geolocations\/4,
"resource" : geolocations
}
}
},
-{
"id" : 5,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/items\/5,
"public" : true,
"featured" : false,
"added" : 2018-07-13T16:28:23+00:00,
"modified" : 2018-07-13T16:29:23+00:00,
"item_type" : -{
"id" : 18,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/item_types\/18,
"name" : Curatescape Story,
"resource" : item_types
},
"collection" : ,
"owner" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/users\/1,
"resource" : users
},
"files" : -{
"count" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/files?item=5,
"resource" : files
},
"tags" : -[
],
"element_texts" : -[
-{
"html" : false,
"text" : 1515 14th Avenue,
"element_set" : -{
"id" : 1,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/1,
"name" : Dublin Core,
"resource" : element_sets
},
"element" : -{
"id" : 50,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/50,
"name" : Title,
"resource" : elements
}
},
-{
"html" : false,
"text" : Greeley Central High School,
"element_set" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/3,
"name" : Item Type Metadata,
"resource" : element_sets
},
"element" : -{
"id" : 52,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/52,
"name" : Subtitle,
"resource" : elements
}
},
-{
"html" : false,
"text" : Street view of Greeley Central High School. The front and side of the building are visible. Written above the entrance, 'Greeley High School.'; Verso Written in blue ink and pencil, 'Greeley Central High School - 14th Ave & 16th St.',
"element_set" : -{
"id" : 3,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/element_sets\/3,
"name" : Item Type Metadata,
"resource" : element_sets
},
"element" : -{
"id" : 54,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/elements\/54,
"name" : Story,
"resource" : elements
}
}
],
"extended_resources" : -{
"exhibit_pages" : -{
"count" : 0,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/exhibit_pages?item=5,
"resource" : exhibit_pages
},
"geolocations" : -{
"id" : 5,
"url" : http:\/\/www.ralstoncemetery.com\/greeley\/api\/geolocations\/5,
"resource" : geolocations
}
}
}
]
我正在处理的大多数示例,教程和阅读材料都具有一个json响应,该响应在最顶部具有一个名称(在这种情况下,它可能是项目),但这直接用于嵌套。
所以我的问题是,我上面的类是否正确设置了格式,或者因为它开始嵌套在它的模式中,所以我需要做一些更改吗?