RestKit错误映射Expedia API

时间:2015-08-25 15:48:13

标签: ios json mapping restkit

尝试使用RestKit从JSON映射深层嵌套对象时出错。首先,我尝试映射正常工作的酒店名称。但是,当我试图从" ChargeableRateInfo"从JSON我得到以下错误:

  

restkit.object_mapping:RKMappingOperation.m:652警告:失败   映射嵌套对象:(null)

这是我想要映射的JSON示例:

{
"HotelListResponse": {
    "customerSessionId": "0ABAAA3E-7637-1591-4F62-5548AC792D42",
    "numberOfRoomsRequested": 1,
    "moreResultsAvailable": true,
    "cacheKey": "-48763715:14f65548ac7:2d4e",
    "cacheLocation": "10.186.170.62:7300",
    "cachedSupplierResponse": {
        "@supplierCacheTolerance": "NOT_SUPPORTED",
        "@cachedTime": "0",
        "@supplierRequestNum": "235",
        "@supplierResponseNum": "1",
        "@supplierResponseTime": "503",
        "@candidatePreptime": "30",
        "@otherOverheadTime": "5",
        "@tpidUsed": "5001",
        "@matchedCurrency": "true",
        "@matchedLocale": "true"
    },
    "HotelList": {
        "@size": "1",
        "@activePropertyCount": "235",
        "HotelSummary": {
            "@order": "0",
            "@ubsScore": "1069912",
            "hotelId": 206005,
            "name": "Mediterranean Inn",
            "address1": "425 Queen Anne Ave N",
            "city": "Seattle",
            "stateProvinceCode": "WA",
            "postalCode": 98109,
            "countryCode": "US",
            "airportCode": "SEA",
            "supplierType": "E",
            "propertyCategory": 1,
            "hotelRating": 3,
            "confidenceRating": 70,
            "amenityMask": 7799051,
            "tripAdvisorRating": 4,
            "tripAdvisorReviewCount": 1086,
            "tripAdvisorRatingUrl": "http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/4.0-12345-4.gif",
            "locationDescription": "Near Pacific Northwest Ballet",
            "shortDescription": "<p><b>Property Location</b> <br />With a stay at Mediterranean Inn, you'll be centrally located in Seattle, steps from Key Arena and minutes from Seattle Children's Theatre. This family-friendly",
            "highRate": 270,
            "lowRate": 162,
            "rateCurrencyCode": "USD",
            "latitude": 47.62244,
            "longitude": -122.35672,
            "proximityDistance": 12.288694,
            "proximityUnit": "MI",
            "hotelInDestination": true,
            "thumbNailUrl": "/hotels/1000000/900000/897600/897598/897598_108_t.jpg",
            "deepLink": "http://www.travelnow.com/templates/55505/hotels/206005/overview?lang=en&currency=USD&standardCheckin=9/15/2015&standardCheckout=9/17/2015&roomsCount=1&rooms[0].adultsCount=2",
            "RoomRateDetailsList": {
                "RoomRateDetails": {
                    "roomTypeCode": 15567,
                    "rateCode": 15567,
                    "maxRoomOccupancy": 2,
                    "quotedRoomOccupancy": 2,
                    "minGuestAge": 0,
                    "roomDescription": "Petite Room, 1 Queen Bed, Kitchenette",
                    "propertyAvailable": true,
                    "propertyRestricted": false,
                    "expediaPropertyId": 897598,
                    "RateInfos": {
                        "@size": "1",
                        "RateInfo": {
                            "@priceBreakdown": "true",
                            "@promo": "true",
                            "@rateChange": "false",
                            "RoomGroup": {
                                "Room": {
                                    "numberOfAdults": 2,
                                    "numberOfChildren": 0,
                                    "rateKey": "81423e04-b223-4ea1-807a-8812c13b31be",
                                    "ChargeableNightlyRates": [
                                        {
                                            "@baseRate": "270.00",
                                            "@rate": "162.00",
                                            "@promo": "true"
                                        }, {
                                            "@baseRate": "270.00",
                                            "@rate": "162.00",
                                            "@promo": "true"
                                        }
                                    ]
                                }
                            },
                            "ChargeableRateInfo": {
                                "@averageBaseRate": "270.00",
                                "@averageRate": "162.00",
                                "@commissionableUsdTotal": "324.00",
                                "@currencyCode": "USD",
                                "@maxNightlyRate": "162.00",
                                "@nightlyRateTotal": "324.00",
                                "@grossProfitOffline": "54.87",
                                "@grossProfitOnline": "81.40",
                                "@surchargeTotal": "69.07",
                                "@total": "393.07",
                                "NightlyRatesPerRoom": {
                                    "@size": "2",
                                    "NightlyRate": [
                                        {
                                            "@baseRate": "270.00",
                                            "@rate": "162.00",
                                            "@promo": "true"
                                        }, {
                                            "@baseRate": "270.00",
                                            "@rate": "162.00",
                                            "@promo": "true"
                                        }
                                    ]
                                },
                                "Surcharges": {
                                    "@size": "1",
                                    "Surcharge": {
                                        "@type": "TaxAndServiceFee",
                                        "@amount": "69.07"
                                    }
                                }
                            },
                            "nonRefundable": false,
                            "rateType": "MerchantStandard",
                            "promoId": 212134483,
                            "promoDescription": "24 hour deal: save 40%",
                            "promoType": "Standard",
                            "currentAllotment": 3
                        }
                    }
                }
            }
        }
    }
}
}

这是我的代码,我尝试映射这个JSON:

- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"http://api.ean.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];


// setup object mappings
RKObjectMapping *hotelMapping = [RKObjectMapping mappingForClass:[HotelList class]];
[hotelMapping addAttributeMappingsFromArray:@[@"name"]];



RKObjectMapping *priceMapping = [RKObjectMapping mappingForClass:[ChargeableRateInfo class]];
[priceMapping addAttributeMappingsFromDictionary:@{@"total": @"total"}];

[hotelMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"RoomRateDetailsList.RoomRateDetails.RateInfos.RateInfo.ChargeableRateInfo" toKeyPath:@"chargeableRateInfo" withMapping:priceMapping]];


// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:hotelMapping
                                             method:RKRequestMethodGET
                                        pathPattern:@"/ean-services/rs/hotel/v3/list"
                                            keyPath:@"HotelListResponse.HotelList.HotelSummary"
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];

[objectManager addResponseDescriptor:responseDescriptor];


}

有谁知道如何解决这个问题?感谢

1 个答案:

答案 0 :(得分:0)

根据JSON中的内容,映射键应为@total

然而,这并不理想,因为@是KKC中的一个特殊字符,RestKit会大量使用它。您需要使用this answerthis pull request中的内容解决此问题。