我无法解码JSON数据。我正在为此API使用get方法,即使用Alamofire。
这是我的登录JSON数据
{
customer = (
{
"customer_comment" = No;
"customer_id" = 128;
"customer_mobile" = 12345678;
"customer_name" = testing;
date = "2019-05-28";
deleted = 0;
"device_token" = "";
"device_type" = iOS;
discount = "No Data";
dob = "2019-05-28";
email = "testing@gmail.com";
gender = Male;
"invoice_no" = 0;
"item_id" = 0;
"lucky_date" = "0000-00-00 00:00:00";
"no_of_attempts" = 0;
notifications = 1;
"on_hold_credit" = 0;
password = 123456;
status = 1;
"total_credits" = 0;
"used_lucky" = No;
"used_weekly" = No;
"weekly_date" = "0000-00-00 00:00:00";
}
);
value = 1;
}
结构
struct MyAccount: Codable {
let value: Int
let customer: [Customer]
}
struct Customer: Codable {
let customerID, itemID, customerMobile, invoiceNo, noOfAttempts, onHoldCredit : Int
let customerName, weeklyDate, usedWeekly: String
let luckyDate, usedLucky, discount: String
let email, gender: String
let customerComment: String
let date, dob: Date
let deviceToken, deviceType : String
let password, notifications, status, deleted, totalCredits: Int
enum CodingKeys: String, CodingKey {
case customerID = "customer_id"
case customerName = "customer_name"
case weeklyDate = "weekly_date"
case usedWeekly = "used_weekly"
case itemID = "item_id"
case luckyDate = "lucky_date"
case usedLucky = "used_lucky"
case discount
case invoiceNo = "invoice_no"
case noOfAttempts = "no_of_attempts"
case email
case customerMobile = "customer_mobile"
case password
case customerComment = "customer_comment"
case date
case deviceToken = "device_token"
case deviceType = "device_type"
case notifications
case totalCredits = "total_credits"
case onHoldCredit = "on_hold_credit"
case status
case deleted
case dob
case gender
}
}
Webserice代码
public func get<T:Codable>(url:String , parameters:Dictionary<String, Any>? ,decodingType:T.Type, onCompletion:@escaping(T?) -> Void, onError:@escaping(String) -> Void) {
if Connectivity.isConnectedToInternet() {
print("Yes! internet is available.")
// do some tasks..
AF.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (responseObject) in
print("API RESULT >> \(url) >> \(responseObject)")
if responseObject.result.isSuccess {
decoderProtocol.decode(data: responseObject.data, decodingType: decodingType, onCompletion: { result in
onCompletion(result)
}, onError: {error in
onError(error)
})
}
else {
onError(responseObject.result.description)
}
}
}
else {
let window = UIApplication.shared.keyWindow
window?.makeToast("No Internet Connection!")
SVProgressHUD.dismiss()
}
}
struct decoderProtocol {
static func decode<T:Codable>(data:Data?, decodingType:T.Type ,onCompletion:@escaping(T) -> Void , onError:@escaping(String) -> Void) {
guard let dataObject = data else {
onError("No Response")
return
}
let decoder = JSONDecoder()
do {
let result = try decoder.decode(T.self, from: dataObject)
onCompletion(result)
}
catch let err{
print(err.localizedDescription)
}
}
}
请有人告诉我如何解决此问题