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



我得到临床零,我已经提到了我的模型和错误消息。

我已经检查并比较了所有密钥与 JSON 响应密钥。

我的 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"
    }
}

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

最新更新