gpt4 book ai didi

ios - 我有一个模型,其中我得到一个键值 nil 但数据来自 json 响应是完美的

转载 作者:行者123 更新时间:2023-11-29 05:43:11 25 4
gpt4 key购买 nike

我得到的诊所为零,我已经提到了我的型号和错误消息。

我已检查所有 key 并将其与 json 响应 key 进行比较。

我的诊所 json 响应中出现错误

"clinic": [
{
"id": 45,
"user_id": "75",
"clinic_id": "2",
"created_at": null,
"updated_at": null,
"clinic": {
"id": 2,
"name": "Al Azhar Hospital",
"status": "1",
"created_at": "2019-04-18 07:01:07",
"updated_at": null
}
}
]
<小时/>
typealias SpecialityDoctor = [SpecialityWiseDoctorResponse]


struct SpecialityWiseDoctorResponse: Codable {
let id: Int
let firstName: String
let lastName: String?
let email: String
let postMail: String?
let mobile, age, password, gender: String
let dateOfBirth: String?
let profilePic, deviceID: String
let language, insuranceCompanyName, insurancePolicyNo: String?
let welcomeDescription: String
let emrNumber: String?
let startTime, endTime: String
let status: Int
let totalSubscriptionMonth, planStartDate, planEndDate, createdAt: String?
let updatedAt: String?
let speciality: [SpecialityElement]
let clinic: [ClinicElementOfDoctor]
let education: [EducationOfDoctor]
let experience: [ExperienceOfDoctor]

enum CodingKeys: String, CodingKey {
case id
case firstName = "first_name"
case lastName = "last_name"
case email
case postMail = "post_mail"
case mobile, age, password, gender
case dateOfBirth = "date_of_birth"
case profilePic = "profile_pic"
case deviceID = "device_id"
case language
case insuranceCompanyName = "insurance_company_name"
case insurancePolicyNo = "insurance_policy_no"
case welcomeDescription = "description"
case emrNumber = "emr_number"
case startTime = "start_time"
case endTime = "end_time"
case status
case totalSubscriptionMonth = "total_subscription_month"
case planStartDate = "plan_start_date"
case planEndDate = "plan_end_date"
case createdAt = "created_at"
case updatedAt = "updated_at"
case speciality, clinic, education, experience
}
}


struct ClinicElementOfDoctor: Codable {
let id: Int
let userID, clinicID: String
let createdAt, updatedAt: String?
let clinic: ClinicClinicOfDoctor

enum CodingKeys: String, CodingKey {
case id
case userID = "user_id"
case clinicID = "clinic_id"
case createdAt = "created_at"
case updatedAt = "updated_at"
case clinic
}
}


struct ClinicClinicOfDoctor: Codable {
let id: Int
let name, status, createdAt: String
let updatedAt: String?

enum CodingKeys: String, CodingKey {
case id, name, status
case createdAt = "created_at"
case updatedAt = "updated_at"
}
}


struct EducationOfDoctor: Codable {
let id: Int
let userID, instituteName, course, year: String
let createdAt, updatedAt: String?

enum CodingKeys: String, CodingKey {
case id
case userID = "user_id"
case instituteName = "institute_name"
case course, year
case createdAt = "created_at"
case updatedAt = "updated_at"
}
}


struct ExperienceOfDoctor: Codable {
let id: Int
let userID, hospitalName, position, year: String
let createdAt, updatedAt: String?

enum CodingKeys: String, CodingKey {
case id
case userID = "user_id"
case hospitalName = "hospital_name"
case position, year
case createdAt = "created_at"
case updatedAt = "updated_at"
}
}


(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "clinic", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"clinic\", intValue: nil) (\"clinic\").", underlyingError: nil)))))

最佳答案

我刚刚尝试了自己的 Web API 响应,我认为问题在于结构中定义的数据类型。

"clinic": {
"id": 2,
"name": "Al Azhar Hospital",
"status": "1",
"created_at": "2019-04-18 07:01:07",
"updated_at": null
}

无论您以数字形式获取哪种数据,都应将其保存为 Int 数据类型,如下所示。

struct ClinicClinicOfDoctor: Codable {
let id, status : Int
let name, createdAt: String
let updatedAt: String?

enum CodingKeys: String, CodingKey {
case id, name, status
case createdAt = "created_at"
case updatedAt = "updated_at"
}
}

请验证响应中的值、数据类型并相应地更改所有模型。希望这对您有所帮助。

关于ios - 我有一个模型,其中我得到一个键值 nil 但数据来自 json 响应是完美的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56375807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com