解析 swift 中的响应时出现问题?

Issue while parsing response in swift?

我正在尝试从 API 获取响应,但缺少一些值,我如何获取这些值,我的响应是这样的,

{
"responseHeader": {
    "code": 1,
    "msg": "Data Retrived Successfully "
},
"responseBody": {
    "data": [
        {
            "totalPages": 1,
            "pageNumber": 0,
            "numberOfElements": 3,
            "totalRecords": 3,
            "data": [
                {
                    "buyerProcessId": 55,
                    "bidReferenceNo": "BidReferenceNo-439187-GEA",
                    "biddingStatus": null,
                    "chiraghuser": {
                        "userId": 1678,
                        "userName": "mutiurrehman111",
                        "userEmail": "muti.urrehman2202@gmail.com",
                        "firstName": "Muti",
                        "middleName": null,
                        "lastName": "urRehman",
                        "mobileNo": "3477002202",
                        "streetAddress": null,
                        "buildingAddress": "Dubai Tower - D89 - Dubai - United Arab Emirates",
                        "emailVerificationCode": null,
                        "mobileOtpCode": "8712",
                        "cityId": 1,
                        "otpCodeExpiration": null,
                        "profilePicUpload": null,
                        "token": "1",
                        "role": "chiraghuser",
                        "nationality": "Pakistani",
                        "idCardNumber": null,
                        "idCardExpiryDate": null,
                        "passportNumber": null,
                        "passportExpiryDate": null,
                        "phoneNumber": null,
                        "fax": null,
                        "classifyYourself": null,
                        "country": null,
                        "userCity": null,
                        "scannedPassportCopyUpload": null,
                        "scannedIdCopyUpload": null,
                        "oldPasssword": null,
                        "status": "activated",
                        "nationalityOther": null,
                        "securityOtp": "4491",
                        "mobileCode": "92",
                        "telephoneCode": null,
                        "countryId": 162,
                        "isApproved": null
                    },
                    "chiraghproperty": {
                        "propertyId": 36567,
                        "propertyTitle": "Jumeirah Village Triangle , 4 Beds, Town House",
                        "propertyDescription": "Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - ",
                        "location": null,
                        "sizePerSqft": 0.0,
                        "address": "Dubai - United Arab Emirates",
                        "askingPrice": 0.0,
                        "areaPermitNo": null,
                        "propertyReferenceNo": "Prop-840146-BFU",
                        "pricePerSqft": 0.0,
                        "isMdApproved": true,
                        "isPublished": null,
                        "noOfBedrooms": 4,
                        "noOfBaths": 3,
                        "floorNo": null,
                        "kitchens": null,
                        "carParks": null,
                        "noOfFloors": null,
                        "propertyTypeId": null,
                        "scannedTitleDeed": null,
                        "mapLocation": "25.0472882,55.190022699999986",
                        "plotNo": null,
                        "titleDeedNo": null,
                        "typeArea": "Non-Free Hold",
                        "projectName": "Jumeirah Village Triangle ",
                        "ownerAssociationNo": null,
                        "presentUse": "Residential",
                        "communityNo": null,
                        "propertyAge": null,
                        "noUnits": null,
                        "noShops": null,
                        "userId": null,
                        "leaseStartDate": null,
                        "leaseExpiryDate": null,
                        "tenantName": null,
                        "tenancyContractUpload": null,
                        "thirdPartyVerification": "true",
                        "isThirdPartyPayment": null,
                        "verificationHodApproved": "1",
                        "valuationHodApproved": false,
                        "isVacant": null,
                        "agreeVatCommission": false,
                        "amount": 0.0,
                        "balanceAmount": 0.0,
                        "propertyNo": null,
                        "propertyStatus": "Off-Plan",
                        "propertyType": "Town House",
                        "area": null,
                        "lastActionPerformed": "Not Started",
                        "isPersonalDetailsVerified": null,
                        "isPoaDetailsVerified": null,
                        "isPropertyDetailsVerified": "true",
                        "isAcknowledgementCall": null,
                        "dateReceived": "2019-10-21",
                        "lastAction": null,
                        "actionPerformedBy": null,
                        "sellerUserName": "Test_User",
                        "status": "active",
                        "areaUnit": "sqft",
                        "isPropertyFinancialDetailsVerified": null,
                        "isPropertyRentalDetailsVerified": null,
                        "bankOther": null,
                        "netArea": "980",
                        "grossArea": "1500",
                        "floorPlanUpload": null,
                        "typePropertyOther": null,
                        "propertyStatusOther": null,
                        "bulidingName": null,
                        "bulidingNumber": null,
                        "isVerificationHodApproved": "false",
                        "valuationAssignTo": null,
                        "brokerageAssignTo": null,
                        "isValuationCompleted": null,
                        "valuationReportCopy": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665049034-1valuation.pdf",
                        "isThirdPartyValuationCompleted": null,
                        "thirdPartyReportCopy": null,
                        "isValuationReportUploaded": "true",
                        "valuationReportDocument": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665045520-1valuation.pdf",
                        "isValuationHodApproved": "1",
                        "isBrokerageCompleted": null,
                        "isBrokerageReportUploaded": "true",
                        "brokerageReportCopy": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665219758-1valuation.pdf",
                        "isBrokerageHodApproved": "1",
                        "price": null,
                        "isWatchedList": null,
                        "auctionStatus": "live",
                        "developerName": "dev",
                        "makaniNo": null,
                        "adminPropertyStatus": "new",
                        "requestedChanges": null,
                        "priceUnit": null,
                        "telephoneCode": null,
                        "mobileCode": null,
                        "listingChecklistConfirm": true,
                        "listingHodApproved": "1",
                        "verificationChecklistConfirm": null,
                        "valuationChecklistConfirm": null,
                        "brokerageChecklistConfirm": null,
                        "financeChecklistConfirm": null,
                        "isFinanceHodApproved": null,
                        "verificationAssignTo": null,
                        "otherDocument": null,
                        "spaDocument": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571664075956-1valuation.pdf",
                        "okoodiNo": "787878787",
                        "netAreaUnit": "sqft",
                        "officeNo": null,
                        "departementId": null,
                        "userRoleId": null,
                        "isOwnerVerified": false,
                        "isPoaVerified": false,
                        "isPropertyVerified": false,
                        "isReportVisibleToSeller": null,
                        "formAUpload": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665216574-1valuation.pdf",
                        "isFormAUploaded": 1,
                        "isValuationReportCopyUploaded": null,
                        "isRegistrationReady": null,
                        "rentalOtherDocument": null,
                        "thirdpartyHodApproved": null,
                        "listingTitle": "Jumeirah Village Triangle , 4 Beds, Town House",
                        "listingDescription": "<p>Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle - Jumeirah Village Triangle -</p>",
                        "buyerProcessId": 0,
                        "images": [
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665096963-95a23be9d1e876b1ae03514493ff69cc_mod_1.jpg",
                                "imageName": "ordinary",
                                "fileName": null,
                                "isVisible": null,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665097160-95a23be9d1e876b1ae03514493ff69cc_mod_1.jpg"
                            },
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665099672-49836-1.jpg",
                                "imageName": "ordinary",
                                "fileName": null,
                                "isVisible": null,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665099909-49836-1.jpg"
                            },
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665102680-292968_16012118210039308812.jpg",
                                "imageName": "ordinary",
                                "fileName": null,
                                "isVisible": true,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665102759-292968_16012118210039308812.jpg"
                            },
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665106366-1804.png",
                                "imageName": "ordinary",
                                "fileName": null,
                                "isVisible": true,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665106475-1804.png"
                            },
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665112768-37605408-800x600.jpeg",
                                "imageName": "ordinary",
                                "fileName": null,
                                "isVisible": null,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665112906-37605408-800x600.jpeg"
                            },
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665117617-130257047.jpg",
                                "imageName": "main-ordinary",
                                "fileName": null,
                                "isVisible": true,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665117694-130257047.jpg"
                            },
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665122815-117127050.jpg",
                                "imageName": "ordinary",
                                "fileName": null,
                                "isVisible": null,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571665122878-117127050.jpg"
                            },
                            {
                                "propertyId": 36567,
                                "imageUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571676983964-10-marla-house-design-luxury-new-indian-exterior-house-designs-s-of-10-marla-house-design.jpg",
                                "imageName": "ordinary",
                                "fileName": null,
                                "isVisible": true,
                                "thumbnailUrl": "https://s3.us-east-2.amazonaws.com/chiragh-asset/1571676984241-10-marla-house-design-luxury-new-indian-exterior-house-designs-s-of-10-marla-house-design.jpg"
                            }
                        ],
                        "propertyBids": null,
                        "country": {
                            "countryId": 0,
                            "name": null,
                            "isActive": null,
                            "nationality": null,
                            "iso2": null,
                            "iso3": null,
                            "code": null,
                            "cities": []
                        },
                        "city": {
                            "cityId": 0,
                            "name": null,
                            "isActive": null
                        }
                    },
                    "processType": "open_house",
                    "paymentStatus": null,
                    "openHouseNotes": null
                }
]
}]}

这是我的模型 class,我正在通过它 json 在我的模型 class,

    import ObjectMapper

class OpenHouseModel: Mappable {

    var totalPages : Int?
    var pageNumber : Int?
    var numberOfElements : Int?
    var totalRecords : Int?
    var propertyData : [PropertyData]?

    required init?(map: Map) {

    }

    func mapping(map: Map) {

        totalPages <- map["totalPages"]
        pageNumber <- map["pageNumber"]
        numberOfElements <- map["numberOfElements"]
        totalRecords <- map["totalRecords"]
        propertyData <- map["data"]
    }
}

class PropertyData: Mappable {

    var buyerProcessId : Int?
    var bidReferenceNo : String?
    var processType : String?
    var chiraghProperty : ChiraghProperty?
    var images : [PropertyImages]?

    required init?(map: Map) {

    }

    func mapping(map: Map) {

        processType <- map["processType"]
        buyerProcessId <- map["buyerProcessId"]
        buyerProcessId <- map["bidReferenceNo"]
        chiraghProperty <- map["chiraghproperty"]
        images <- map["images"]
    }
}


class ChiraghProperty: Mappable {

    var propertyId : Int?
    var propertyTitle : String?
    var propertyDescription : String?
    var sizePerSqft : Double?
    var address : String?
    var propertyReferenceNo : String?
    var pricePerSqft : Double?
    var noOfBedrooms : Int?
    var noOfBaths : Int?
    var typeArea : String?
    var projectName : String?
    var presentUse : String?
    var propertyType : String?
    var dateReceived : String?
    var areaUnit : String?
    var status : String?
    var netArea : String?
    var grossArea : String?
    var valuationReportCopy : String?
    var valuationReportDocument: String?
    var brokerageReportCopy: String?
    var spaDocument : String?
    var formAUpload : String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {

        propertyId <- map["propertyId"]
        propertyTitle <- map["propertyTitle"]
        propertyDescription <- map["propertyDescription"]
        sizePerSqft <- map["sizePerSqft"]
        address <- map["address"]
        propertyReferenceNo <- map["propertyReferenceNo"]
        pricePerSqft <- map["pricePerSqft"]
        noOfBedrooms <- map["noOfBedrooms"]
        noOfBaths <- map["noOfBaths"]
        typeArea <- map["typeArea"]
        projectName <- map["projectName"]
        propertyType <- map["propertyType"]
        dateReceived <- map["dateReceived"]
        areaUnit <- map["areaUnit"]
        status <- map["status"]
        netArea <- map["netArea"]
        valuationReportCopy <- map["valuationReportCopy"]
        valuationReportDocument <- map["valuationReportDocument"]
        brokerageReportCopy <- map["brokerageReportCopy"]
        spaDocument <- map["spaDocument"]
        formAUpload <- map["formAUpload"]

    }
}


class PropertyImages: Mappable {

    var imageUrl : String?
    var imageName : String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {

        imageUrl <- map["imageUrl"]
        imageName <- map["imageName"]
    }
}

现在,当我 运行 代码并获取值时,我得到 buyerProcessId 键的 nil。 其他值都很好,我不知道为什么它不显示 chiraghuser.

以上这些键的值

您的问题似乎出在 PropertyData 中,因为您似乎正在将 bidReferenceNo 映射到 buyerProcessId。这就是您获得 nil 的原因。

class PropertyData: Mappable {

    var buyerProcessId : Int?
    var bidReferenceNo : String?
    var processType : String?
    var chiraghProperty : ChiraghProperty?
    var images : [PropertyImages]?

    required init?(map: Map) {

    }

    func mapping(map: Map) {

        processType <- map["processType"]
        buyerProcessId <- map["buyerProcessId"]
        buyerProcessId <- map["bidReferenceNo"] // this seems like it is wrong and should be bidReferenceNo
        chiraghProperty <- map["chiraghproperty"]
        images <- map["images"]
    }
}

由于您使用的是 ObjectMapper,因此可以使用 BetterMappable 减少大量样板代码,这些代码是使用 PropertyWrappers 在 ObjectMapper 上编写的。您需要使用 Swift 5.1 才能使用它。

在代码库中使用 BetterMappable 后,您的模型将如下所示。

class PropertyData: Mappable {

    @JSONProperty var buyerProcessId : Int?
    @JSONProperty var bidReferenceNo : String?
    @JSONProperty var processType : String?
    @JSONObject var chiraghProperty : ChiraghProperty?
    @JSONObjectArray var images : [PropertyImages]?

    required init?(map: Map) {

    }
}