Using Swift4, iOS11.1, Xcode9.1,
Trying to match the JSON-file in a codable struct for Swift4, I have the following issue:
Here is my code:
struct Station: Codable {
let htmlAttributions: [String]
let nextPageToken: String
let status: String
struct Result: Codable {
let id: String
enum CodingKeys : String, CodingKey {
case id
}
}
enum CodingKeys : String, CodingKey {
case htmlAttributions = "html_attributions"
case nextPageToken = "next_page_token"
case status
case result
}
}
From what I understood, I have to place the Enum in order to allow for underline-keys that occur in the JSON-File. But after adding the Enum, the error message crops up. What is wrong with my Enum ? How is it placed correctly ?
Can you get me a go on the following JSON-File? How would I have to match it in a struct ? Any help appreciated !
Here is the JSON-File:
{
"html_attributions" : [],
"next_page_token" : "F3ddaOzOcyo94AA2skDm",
"results" : [
{
"formatted_address" : "Strasse 1, 6003 Luzern, Switzerland",
"geometry" : {
"location" : {
"lat" : 47.04951260000001,
"lng" : 8.310404999999999
},
"viewport" : {
"northeast" : {
"lat" : 47.0508615802915,
"lng" : 8.311753980291503
},
"southwest" : {
"lat" : 47.0481636197085,
"lng" : 8.309056019708498
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "a3d600a6e78105b6ce2b5f5a3fac98ca1910a09b",
"name" : "Luzern",
"photos" : [
{
"height" : 4000,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/113951418385089253589/photos\"\u003eAlex Marcu\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAYHK1VHDFlkbzXuMnF2MLEdew-36lgHC2lS1Cxg_DELgP-ckZH7G6aa-81LGDpR5rPZY1XMw64mytsjXIrdB5n3QQmXjGgelwZEbHaetT2jpy9SeaHDH3qUGGAUW-7BtZEhCxXy2dxGSv6A_g7fipsCr5GhRZlPuliykokXIkqfqIN_vMWzmYyA",
"width" : 3000
}
],
"place_id" : "ChIJqQIbhpj7j0cRjUguIM__gZw",
"rating" : 4.4,
"reference" : "CmRSAAAAzBZCshpypXcbMhrBQIdK2zISd3Q40QRSFO0KKhIrTejnGiZIoASuVqCVtmNBnFsodLWrYtOP-RmwCqDBDVbMheeCbFk7f0L8gwixLx_SGhYTDqPd6B2IwPWWXH5Pb6lxEhBoQtWj-kB-g1ZiOZ74hswNGhSd9Kf9Qj1P2_fdQCTO_VCoTU09JA",
"types" : [
"transit_station",
"bus_station",
"train_station",
"point_of_interest",
"establishment"
]
},
{ ...more results... },
{ ...more results... }
],
"status" : "OK"
}
Is your struct Station
correctly like this?
struct Station: Codable {
let htmlAttributions: [String]
let nextPageToken: String
let status: String
let result: Result
struct Result: Codable {
let id: String
enum CodingKeys : String, CodingKey {
case id
}
}
enum CodingKeys : String, CodingKey {
case htmlAttributions = "html_attributions"
case nextPageToken = "next_page_token"
case status
case result = "resulsts"
}
}
Complete this thing in the pattern I did and it will work fine. It works for me all the time!
struct Station: Codable {
let next_page_token: String?
let results: [Results]
}
struct Results: Codable {
let formatted_address: String?
let geometry: Geometry
let icon: String?
let id: String?
let name: String?
let photos: [Photos]
}
struct Geometry: Codable {
let location: Location
let viewport: Viewport
}
struct Location: Codable {
let lat: Float?
let lng: Float?
}
struct Viewport: Codable {
let northeast: Location
let southwest: Location
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.