1

我想使用 RestKit 0.20 反序列化以下 JSON 响应,但它失败并且腿被定义为NSSet并且总是返回nil。这段代码有什么问题?

{
"Flights": [{
    "Id": "1",
    "Price": 100,
    "Duration": 60,
    "RemainingSeats": 10,
    "Legs": [{
        "Airline": "Tukish Airlines",
        "FlightNumber": "A111",
        "TakeOffDate": "\/Date(1355915405327)\/",
        "TakeOffAirport": "AHL",
        "LandingDate": "\/Date(1356001805327)\/",
        "LandingAirport": "ERC",
        "Duration": 60,
        "TransportDuration": 0
    }]
}]
}

RKObjectMapping* legMapping = [RKObjectMapping mappingForClass:[Leg class]];
[legMapping addAttributeMappingsFromDictionary:@{
 @"Airline": @"airline",
 @"FlightNumber": @"flightNumber",
 @"TakeOffDate": @"takeOffDate",
 @"TakeOffAirport": @"takeOffAirport",
 @"LandingDate": @"landingDate",
 @"LandingAirport": @"landingAirport",
 @"Duration": @"duration",
 @"TransportDuration": @"transportDuration",
 }];

RKObjectMapping* flightMapping = [RKObjectMapping mappingForClass:[Flight class]];
[flightMapping addAttributeMappingsFromDictionary:@{
 @"Id": @"id",
 @"Price": @"price",     
 @"Duration": @"duration",     
 @"RemainingSeats": @"remainingSeats"
 }];

RKRelationshipMapping *flightLegsMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Legs" toKeyPath:@"legs" withMapping:legMapping];
[flightMapping addRelationshipMappingWithSourceKeyPath:@"legs" mapping:flightLegsMapping.mapping];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:flightMapping pathPattern:nil keyPath:@"Flights" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

NSString *isRoundTripString = isRoundTrip ? @"true" : @"false";

NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc] init];
[gmtFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *departureDateString = [gmtFormatter stringFromDate:departureDate];
NSString *returnDateString = [gmtFormatter stringFromDate:returnDate];

NSString *directFlightsOnlyString = directFlightsOnly ? @"true" : @"false";

NSString *urlString = [NSString stringWithFormat: @"http://parallels-win7:8132/flight/flights?isRoundTrip=%@&departureAirport=%@&arrivalAirport=%@&departureDate=%@&returnDate=%@&adultCount=%d&childCount=%d&babyCount=%d&oldPeopleCount=%d&studentCount=%d&directFlightsOnly=%@", isRoundTripString, departureAirport, arrivalAirport, departureDateString, returnDateString, adultCounts, childCount, babyCount, oldPeopleCount, studentCount, directFlightsOnlyString];

NSURL *url = [NSURL URLWithString: urlString];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    success(mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    failure(error);
}];

[objectRequestOperation start];

mappingResult.array满了,但腿是空的。

4

0 回答 0