我正在创建一个需要从API提取数据的应用程序,情况是,我将在下面获取json
数据:
[
{
"chargeId": "33fbbbd0-2e33-11e9-a2cb-8a27ecbbcb73",
"chargeDate": "2019-02-12T03:28:44.000",
"vatRate": "NON-VAT",
"taxRate": 0.1,
"policyGroup": "Patient Participation",
"itemDescription": "Ecg at er/icu df",
"scdFlag": 0,
"amounts": null,
"scdDiscounts": 0,
"othDiscounts": 4.54,
"adjustment": 0,
"pfBill": 222.46,
"vatAmount": 0,
"taxableAmount": 11.12,
"merchantDiscount": 0,
"creditedAmount": 211.3,
"chargeAmount": null,
"previousCredits": null
},
{
"chargeId": "5a2cabc1-46c9-11e9-a2cf-863c7cdffd18",
"chargeDate": "2019-03-15T10:24:21.000",
"vatRate": "NON-VAT",
"taxRate": 0.1,
"policyGroup": "Patient Participation",
"itemDescription": "Professional Fees",
"scdFlag": 0,
"amounts": null,
"scdDiscounts": 0,
"othDiscounts": 0,
"adjustment": 0,
"pfBill": 1000,
"vatAmount": 0,
"taxableAmount": 100,
"merchantDiscount": 0,
"creditedAmount": 900,
"chargeAmount": null,
"previousCredits": null
}
]
我确实使用下面的Alamofire
代码成功提取了数据:
typealias getPatientDetailsPerPayoutTaskCompletion = (_ patientDetailsPerPayout: [PatientPayoutDetails]?, _ error: NetworkError?) -> Void
static func getPatientDetailsPerPayout(periodId: Int, doctorNumber: String, parameterName: PatientParameter, hospitalNumber: String, completion: @escaping getPatientDetailsPerPayoutTaskCompletion) {
guard let patientDetailsPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPayoutDetails)?periodId=\(periodId)&doctorNumber=\(doctorNumber)\(parameterName.rawValue)\(hospitalNumber)") else {
completion(nil, .invalidURL)
return
}
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getAllTasks { (tasks) in
tasks.forEach({ $0.cancel() })
}
Alamofire.request(patientDetailsPerPayoutURL, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in
print(patientDetailsPerPayoutURL)
guard HelperMethods.reachability(responseResult: response.result) else {
completion(nil, .noNetwork)
return
}
guard let statusCode = response.response?.statusCode else {
completion(nil, .noStatusCode)
return
}
switch(statusCode) {
case 200:
guard let jsonData = response.data else {
completion(nil, .invalidJSON)
return
}
let decoder = JSONDecoder()
do {
let patientDetailsPayout = try decoder.decode([PatientPayoutDetails].self, from: jsonData)
completion(patientDetailsPayout, nil)
} catch {
completion(nil, .invalidJSON)
}
case 400: completion(nil, .badRequest)
case 404: completion(nil, .noRecordFound)
default:
print("**UNCAPTURED STATUS CODE FROM (getPatientDetailsPayout)\nSTATUS CODE: \(statusCode)")
completion(nil, .uncapturedStatusCode)
}
}
JSON Data
将显示在CollectionCell
中,用户将点击cell
以查看一个chargedId
下的数据,但是不幸的是,当我点击{{1 }}提取所有数据,而不是仅提取数组的一部分。下面的代码是我用来提取数组的一部分的代码:
cell
Didselect提取数据的功能
typealias getSelectedPatientItemDetailsTaskCompletion = (_ selectedpatient: PatientPaymentDetails?, _ error: NetworkError?) -> Void
static func getPatientItemDetails(periodId: Int, doctorNumber: String, parameterName: PatientParameter, hospitalNumber: String, chargeId: String, completion: @escaping getSelectedPatientItemDetailsTaskCompletion) {
guard let patientDetailsPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPayoutDetails)?periodId=\(periodId)&doctorNumber=\(doctorNumber)\(parameterName.rawValue)\(hospitalNumber)") else {
completion(nil, .invalidURL)
return
}
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getAllTasks { (tasks) in
tasks.forEach({ $0.cancel() })
}
Alamofire.request(patientDetailsPerPayoutURL, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in
print(patientDetailsPerPayoutURL)
guard HelperMethods.reachability(responseResult: response.result) else {
completion(nil, .noNetwork)
return
}
guard let statusCode = response.response?.statusCode else {
completion(nil, .noStatusCode)
return
}
switch(statusCode) {
case 200:
guard let jsonData = response.data else {
completion(nil, .invalidJSON)
return
}
let decoder = JSONDecoder()
do {
let patientDetailsPayout = try decoder.decode(PatientPaymentDetails.self, from: jsonData)
completion(patientDetailsPayout, nil)
} catch {
completion(nil, .invalidJSON)
}
case 400: completion(nil, .badRequest)
case 404: completion(nil, .noRecordFound)
default:
print("**UNCAPTURED STATUS CODE FROM (getPatientDetailsPayout)\nSTATUS CODE: \(statusCode)")
completion(nil, .uncapturedStatusCode)
}
}
}
}
}
getItemDetails函数
switch indexPath.section {
case 0:
self.selectedCardIndex = indexPath
let selectedItem = selectedItemDescription.id
getItemDetails(parameter: .searchByChargedId, from: selectedItem)
let cardController = UserCardViewController.init(nibName: "UserCardViewController", bundle: nil)
present(cardController, animated: true, completion: nil)
default: break
}
}
希望您能为我提供帮助,对不起,如果我包括了几乎所有代码,但我只想向您展示代码的流程。进行了将近1周的工作。谢谢。
答案 0 :(得分:0)
函数getPatientItemDetails
和getPatientDetailsPerPayout
都从同一URL检索数据:
guard let patientDetailsPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPayoutDetails)?periodId=\(periodId)&doctorNumber=\(doctorNumber)\(parameterName.rawValue)\(hospitalNumber)") else {
对于不同的端点,您可能具有不同的URL;验证两种方法的URL,以确保您使用适当的方法。