Swift错误:由于格式不正确,无法读取数据

时间:2019-10-03 05:48:03

标签: json swift

将JSON响应映射到模型时遇到一些问题。收到错误后,尽管我从quicktype.io创建了一个模型类,但仍说“类型不匹配”。这是我的模型课。我已经读过很多关于同一标题的问题,但是我找不到任何帮助。

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let responseCustomerInfiormation = try? newJSONDecoder().decode(ResponseCustomerInfiormation.self, from: jsonData)

import Foundation

// MARK: - ResponseCustomerInfiormation
class ResponseCustomerInfiormation: Codable {
    let status: Bool?
    let message: String?
    let responseDescription: ResponseDescription?
    let encryptStatus: Bool?

    enum CodingKeys: String, CodingKey {
        case status = "Status"
        case message = "Message"
        case responseDescription = "ResponseDescription"
        case encryptStatus = "EncryptStatus"
    }

required init(from decoder: Decoder) throws{
        let values = try decoder.container(keyedBy: CodingKeys.self)
        status = try values.decodeIfPresent(Bool.self,forKey: .status)
        message = try values.decodeIfPresent(String.self,forKey: .message)
        responseDescription = try values.decodeIfPresent(ResponseDescription.self,forKey: .responseDescription)
        encryptStatus = try values.decodeIfPresent(Bool.self, forKey: .encryptStatus)

    }

    class ResponseDescription: Codable {
        let id: Int?
        let customerWebID: Int?
        let customerWalletID: Int?
        let cellNo: String?
        let firstName: String?
        let lastName: String?
        let dateOfBirth: String?
        let pincode: String?
        let password: String?
        let cnic: String?
        let emiratesExpiry: String?
        let email: String?
        let walletCard: String?
        let kkCard: String?
        let country: String?
        let countryID: Int?
        let countryOfBirth: String?
        let homeCountryCellNo: String?
        let city: String?
        let cityID: Int?
        let residentOrVisitor: Int?
        let nationalityID: Int?
        let nationality: String?
        let profilePicture: String?
        let regionID: Int?
        let address: String?
        let themeColor: Int?
        let vendorID: Int?
        let vendorImage: String?
        let sahulatWallet: String?
        let karachiKingWallet: String?
        let qrCode: String?
        let valueBack: String?
        let promotionalValueBack: String?
        let aryCoin: String?
        let lockedTopup: String?
        let sahulatComitiAmount: String?
        let sonaComitiAmount: String?
        let sonaComitiGold: String?
        let milliGoldRedeemable: String?
        let milliGoldUnRedeemable: String?
        let milliGoldAmountUnRedeemable: String?
        let sahulatWalletCardRegistrationFeePaid: String?
        let karachiKingCardRegistrationFeePaid: String?
        let totalMonthlyFeeDueSahulatCard: String?
        let totalMonthlyFeeDueKKCard: String?
        let totalMonthlyFeePaid: String?
        let totalMonthlyFeeDue: String?
        let dollarDealPurchaseAmount: String?
        let dollarDealValueBack: String?
        let dollarDealPromotionalValueBack: String?
        let tokenNo: String?
        let encryptionKey: String?
        let responseStatus: Bool?
        let responseMessage: String?

        enum CodingKeys: String, CodingKey {
            case id = "ID"
            case customerWebID = "CustomerWebID"
            case customerWalletID = "CustomerWalletID"
            case cellNo = "CellNo"
            case firstName = "FirstName"
            case lastName = "LastName"
            case dateOfBirth = "DateOfBirth"
            case pincode = "Pincode"
            case password = "Password"
            case cnic = "CNIC"
            case emiratesExpiry = "EmiratesExpiry"
            case email = "Email"
            case walletCard = "WalletCard"
            case kkCard = "KKCard"
            case country = "Country"
            case countryID = "CountryID"
            case countryOfBirth = "CountryOfBirth"
            case homeCountryCellNo = "HomeCountryCellNo"
            case city = "City"
            case cityID = "CityID"
            case residentOrVisitor = "ResidentOrVisitor"
            case nationalityID = "NationalityID"
            case nationality = "Nationality"
            case profilePicture = "ProfilePicture"
            case regionID = "RegionID"
            case address = "Address"
            case themeColor = "ThemeColor"
            case vendorID = "VendorID"
            case vendorImage = "VendorImage"
            case sahulatWallet = "SahulatWallet"
            case karachiKingWallet = "KarachiKingWallet"
            case qrCode = "QRCode"
            case valueBack = "ValueBack"
            case promotionalValueBack = "PromotionalValueBack"
            case aryCoin = "ARYCoin"
            case lockedTopup = "LockedTopup"
            case sahulatComitiAmount = "SahulatComitiAmount"
            case sonaComitiAmount = "SonaComitiAmount"
            case sonaComitiGold = "SonaComitiGold"
            case milliGoldRedeemable = "MilliGoldRedeemable"
            case milliGoldUnRedeemable = "MilliGoldUnRedeemable"
            case milliGoldAmountUnRedeemable = "MilliGoldAmountUnRedeemable"
            case sahulatWalletCardRegistrationFeePaid = "SahulatWalletCardRegistrationFeePaid"
            case karachiKingCardRegistrationFeePaid = "KarachiKingCardRegistrationFeePaid"
            case totalMonthlyFeeDueSahulatCard = "TotalMonthlyFeeDueSahulatCard"
            case totalMonthlyFeeDueKKCard = "TotalMonthlyFeeDueKKCard"
            case totalMonthlyFeePaid = "TotalMonthlyFeePaid"
            case totalMonthlyFeeDue = "TotalMonthlyFeeDue"
            case dollarDealPurchaseAmount = "DollarDealPurchaseAmount"
            case dollarDealValueBack = "DollarDealValueBack"
            case dollarDealPromotionalValueBack = "DollarDealPromotionalValueBack"
            case tokenNo = "TokenNo"
            case encryptionKey = "EncryptionKey"
            case responseStatus = "ResponseStatus"
            case responseMessage = "ResponseMessage"
        }

        required init(from decoder: Decoder) throws{
            let values = try decoder.container(keyedBy: CodingKeys.self)

            id = try values.decodeIfPresent(Int.self, forKey: .id)
            customerWebID = try values.decodeIfPresent(Int.self, forKey: .customerWebID)
            customerWalletID = try values.decodeIfPresent(Int.self, forKey: .customerWalletID)
            cellNo = try values.decodeIfPresent(String.self, forKey: .cellNo)
            firstName = try values.decodeIfPresent(String.self, forKey: .firstName)
            lastName = try values.decodeIfPresent(String.self, forKey: .lastName)
            dateOfBirth = try values.decodeIfPresent(String.self, forKey: .dateOfBirth)
            pincode = try values.decodeIfPresent(String.self, forKey: .pincode)
            password = try values.decodeIfPresent(String.self, forKey: .password)
            cnic = try values.decodeIfPresent(String.self, forKey: .cnic)
            emiratesExpiry = try values.decodeIfPresent(String.self, forKey: .emiratesExpiry)
            email = try values.decodeIfPresent(String.self, forKey: .email)
            walletCard = try values.decodeIfPresent(String.self, forKey: .walletCard)
            kkCard = try values.decodeIfPresent(String.self, forKey: .kkCard)
            country = try values.decodeIfPresent(String.self, forKey: .country)
            countryID = try values.decodeIfPresent(Int.self, forKey: .countryID)
            countryOfBirth = try values.decodeIfPresent(String.self, forKey: .countryOfBirth)
            homeCountryCellNo = try values.decodeIfPresent(String.self, forKey: .homeCountryCellNo)
            city = try values.decodeIfPresent(String.self, forKey: .city)


            cityID = try values.decodeIfPresent(Int.self, forKey: .cityID)
            residentOrVisitor = try values.decodeIfPresent(Int.self, forKey: .residentOrVisitor)
            nationalityID = try values.decodeIfPresent(Int.self, forKey: .nationalityID)
            nationality = try values.decodeIfPresent(String.self, forKey: .nationality)
            profilePicture = try values.decodeIfPresent(String.self, forKey: .profilePicture)
            regionID = try values.decodeIfPresent(Int.self, forKey: .regionID)
            address = try values.decodeIfPresent(String.self, forKey: .address)
            themeColor = try values.decodeIfPresent(Int.self, forKey: .themeColor)
            vendorID = try values.decodeIfPresent(Int.self, forKey: .vendorID)
            vendorImage = try values.decodeIfPresent(String.self, forKey: .vendorImage)
            sahulatWallet = try values.decodeIfPresent(String.self, forKey: .sahulatWallet)
            karachiKingWallet = try values.decodeIfPresent(String.self, forKey: .karachiKingWallet)
             qrCode = try values.decodeIfPresent(String.self, forKey: .qrCode)
            valueBack = try values.decodeIfPresent(String.self, forKey: .valueBack)
            promotionalValueBack = try values.decodeIfPresent(String.self, forKey: .promotionalValueBack)
            aryCoin = try values.decodeIfPresent(String.self, forKey: .aryCoin)
            lockedTopup = try values.decodeIfPresent(String.self, forKey: .lockedTopup)
            sahulatComitiAmount = try values.decodeIfPresent(String.self, forKey: .sahulatComitiAmount)
            sonaComitiAmount = try values.decodeIfPresent(String.self, forKey: .sonaComitiAmount)
            sonaComitiGold = try values.decodeIfPresent(String.self, forKey: .sonaComitiGold)
            milliGoldRedeemable = try values.decodeIfPresent(String.self, forKey: .milliGoldRedeemable)
            milliGoldUnRedeemable = try values.decodeIfPresent(String.self, forKey: .milliGoldUnRedeemable)
            milliGoldAmountUnRedeemable = try values.decodeIfPresent(String.self, forKey: .milliGoldAmountUnRedeemable)
            sahulatWalletCardRegistrationFeePaid = try values.decodeIfPresent(String.self, forKey: .sahulatWalletCardRegistrationFeePaid)
            karachiKingCardRegistrationFeePaid = try values.decodeIfPresent(String.self, forKey: .karachiKingCardRegistrationFeePaid)

            totalMonthlyFeeDueSahulatCard = try values.decodeIfPresent(String.self, forKey: .totalMonthlyFeeDueSahulatCard)
            totalMonthlyFeeDueKKCard = try values.decodeIfPresent(String.self, forKey: .totalMonthlyFeeDueKKCard)
            totalMonthlyFeePaid = try values.decodeIfPresent(String.self, forKey: .totalMonthlyFeePaid)
            totalMonthlyFeeDue = try values.decodeIfPresent(String.self, forKey: .totalMonthlyFeeDue)
            dollarDealPurchaseAmount = try values.decodeIfPresent(String.self, forKey: .dollarDealPurchaseAmount)
            dollarDealValueBack = try values.decodeIfPresent(String.self, forKey: .dollarDealValueBack)
            dollarDealPromotionalValueBack = try values.decodeIfPresent(String.self, forKey: .dollarDealPromotionalValueBack)
            tokenNo = try values.decodeIfPresent(String.self, forKey: .tokenNo)
            encryptionKey = try values.decodeIfPresent(String.self, forKey: .encryptionKey)
            responseStatus = try values.decodeIfPresent(Bool.self, forKey: .responseStatus)
            responseMessage = try values.decodeIfPresent(String.self, forKey: .responseMessage)
        }
    }

}

// MARK: - ResponseDescription

我收到的JSON是

{
  "Status": true,
  "Message": "Customer Information Found",
  "ResponseDescription": {
    "ID": 11111,
    "CustomerWebID": 324,
    "CustomerWalletID": 1234,
    "CellNo": "",
    "FirstName": "mujtuba",
    "LastName": " Amin",
    "DateOfBirth": "19000-01-01",
    "Pincode": "8120",
    "Password": "",
    "CNIC": "",
    "EmiratesExpiry": "",
    "Email": "",
    "WalletCard": "",
    "KKCard": "",
    "Country": "United Arab Emirates",
    "CountryID": 0,
    "CountryOfBirth": "",
    "HomeCountryCellNo": "00",
    "City": "",
    "CityID": 0,
    "ResidentOrVisitor": 1,
    "NationalityID": 0,
    "Nationality": "Pakistani",
    "ProfilePicture": " ",
    "RegionID": 2,
    "Address": "dd",
    "ThemeColor": 0,
    "VendorID": 0,
    "VendorImage": "",
    "SahulatWallet": "1",
    "KarachiKingWallet": "0",
    "QRCode": "https://chart.googleapis.com/chart?chs=400x400&cht=qr&chld=h&chl=970657|2|2345|941A3E84-B1EB-4B9B-9141-71227844A5B9",
    "ValueBack": "3",
    "PromotionalValueBack": "3",
    "ARYCoin": "1000",
    "LockedTopup": "0",
    "SahulatComitiAmount": "0",
    "SonaComitiAmount": "0",
    "SonaComitiGold": "0",
    "MilliGoldRedeemable": "3656",
    "MilliGoldUnRedeemable": "0",
    "MilliGoldAmountUnRedeemable": "0",
    "SahulatWalletCardRegistrationFeePaid": "0",
    "KarachiKingCardRegistrationFeePaid": "0",
    "TotalMonthlyFeeDueSahulatCard": "0",
    "TotalMonthlyFeeDueKKCard": "0",
    "TotalMonthlyFeePaid": "0",
    "TotalMonthlyFeeDue": "0",
    "DollarDealPurchaseAmount": "0",
    "DollarDealValueBack": "0",
    "DollarDealPromotionalValueBack": "0",
    "TokenNo": "0",
    "EncryptionKey": "",
    "ResponseStatus": true,
    "ResponseMessage": "Customer Details Found"
  },
  "EncryptStatus": true
}

任何人都可以指导我哪里出错或代码的哪一部分出了问题。

2 个答案:

答案 0 :(得分:0)

您的json响应格式不正确。

"CustomerWalletID": 0000 // this is causing problem
"CustomerWalletID": 0, // try this

参考:-This will show you if your json is valid or not

答案 1 :(得分:-1)

我猜初始化器有问题,请尝试这种类型的初始化器

required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.name = try container.decodeIfPresent(String.self, forKey: .name)
    ...
}