admin管理员组文章数量:1122846
I would really appreciate some help. I have the following JSON returning from a laravel API backend
[
{
"id": 1,
"name": "instructor",
"hourly_rate": 35.01,
"bio": "Patient and experienced driving instructor specialising in new drivers",
"profile_image": null,
"rating": 4.8,
"total_reviews": 25,
"location": {
"latitude": 51.5074,
"longitude": -0.1278
},
"qualifications": [
"ADI Certificate",
"Grade A Instructor"
],
"years_experience": 5,
"gender": "female",
"transmission": "Manual",
"vehicle_type": null,
"lesson_types": [
"standard",
"intensive",
"pass_plus"
],
"is_available": true
}
]
and the following model
struct Instructor: Identifiable, Codable {
let id: Int
let name: String
let rating: Double
let hourlyRate: Double
let experience: Int
let profileImage: String?
let description: String
let availability: [Availability]
let gender: InstructorGender
let lessonTypes: [LessonType]
let transmission: TransmissionType
let vehicleType: String?
let qualifications: [String]
let isAvailable: Bool
struct Availability: Codable {
let dayOfWeek: Int
let startTime: Date
let endTime: Date
}
enum TransmissionType: String, Codable {
case manual = "Manual"
case automatic = "Automatic"
}
enum LessonType: String, Codable {
case standard = "standard"
case intensive = "intensive"
case passPlus = "pass_plus"
}
enum InstructorGender: String, Codable {
case male
case female
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(String.self).lowercased()
switch rawValue {
case "male": self = .male
case "female": self = .female
default:
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid gender value: \(rawValue)")
}
}
}
enum CodingKeys: String, CodingKey {
case id
case name
case rating
case hourlyRate = "hourly_rate"
case experience = "years_experience"
case profileImage = "profile_image"
case description = "bio"
case availability
case gender
case lessonTypes = "lesson_types"
case transmission
case vehicleType = "vehicle_type"
case qualifications
case isAvailable = "is_available"
}
}
When i use the the below decoder I get the error
ecoding error: keyNotFound(CodingKeys(stringValue: "hourly_rate", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "hourly_rate", intValue: nil) ("hourly_rate").", underlyingError: nil))
private let jsonDecoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}()
func getBookings(token: String) async throws -> [Booking] {
guard let url = URL(string: "\(baseURL)/bookings") else {
throw NetworkError.invalidURL
}
let data = try await sendAuthenticatedRequestWithData(to: url, method: .get, token: token)
if let jsonString = String(data: data, encoding: .utf8) {
print("
本文标签:
Swift JSON Decoding error wth coding Key usageStack Overflow
版权声明:本文标题:Swift JSON Decoding error wth coding Key usage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1736304520a1932265.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论