RESTKit:映射JSON而不映射类

时间:2014-04-22 04:36:36

标签: ios mapping restkit restkit-0.20

以下是我回复的JSON响应:

{
    "notificationId": 121,
    "activities": [
    143, 
    149]
}

以下是映射:

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
    [mapping addAttributeMappingsFromDictionary:@{@"notificationId" : @"notificationId",
                                                  @"activities": @"activities"}];

 RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:nil
                                                                                           keyPath:@""
                                                                                       statusCodes:statusCodeSet];


[self.objectManager postObject:invitation path:@"/notifications" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    NSString *notificationId1 = [mappingResult.dictionary valueForKey:@"notificationId"];
    NSArray *activitesArray1 = [mappingResult.dictionary valueForKey:@"activities"];

    NSString *notificationId2 = [mappingResult.dictionary objectForKey:@"notificationId"];
    NSArray *activitesArray2 = [mappingResult.dictionary objectForKey:@"activities"];

    NSLog(@"notificaiton ID %@", notificationId1);
    NSLog(@"Activites %@", activitesArray1);

    NSLog(@"notification ID %@", notificationId2);
    NSLog(@"Activites %@", activitesArray2);

    NSLog(@"Activites Array %@", activitesArray);


    NSLog(@"mappingResult Dictionary %@", mappingResult.dictionary);


} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failusre");
}];

}

日志:

2014-04-21 19:17:51.273 App[57446:4403] I restkit.network:RKObjectRequestOperation.m:250 POST 'http://www.domain.com/notifications' (200 OK / 1 objects) [request=0.2371s mapping=0.0035s total=0.2446s]
2014-04-21 19:17:51.274 App[57446:60b] notification ID (null)
2014-04-21 19:17:51.274 App[57446:60b] Activites (null)
2014-04-21 19:17:51.274 App[57446:60b] notification ID (null)
2014-04-21 19:17:51.274 App[57446:60b] Activites (null)
2014-04-21 19:17:51.274 App[57446:60b] Activites Array (
)
2014-04-21 19:17:51.275 App[57446:60b] mappingResult Dictionary {
    "" =     {
        activities =         (
            143,
            149
        );
        notificationId = 121;
    };
}

为什么所有值都为空?如何将notificationId的值作为NSString和activities返回到数组中?这可以在不创建映射类的情况下完成吗?

2 个答案:

答案 0 :(得分:3)

因为您的响应描述符是错误的。它应该是:

[RKResponseDescriptor responseDescriptorWithMapping:mapping
                                             method:RKRequestMethodPOST
                                        pathPattern:@"/notifications"
                                            keyPath:nil
                                        statusCodes:statusCodeSet];

以便它与您发送的请求相匹配(POST,而非GET是主要问题)。

答案 1 :(得分:1)

出于一些不明原因,看起来,字典中有一个包装器对象,它具有键值@"",因此首先提取它,然后提取其他所有内容。活动本身看起来像数组,所以简单:

NSArray *activities = [[mappingResult.dictionary objectForKey:@""] objectForKey:@"activities"];

然而通知看起来很棘手,因为它可能是一个数字。因此,如果它是一致的类型,请使用:

NSNumber *notificationID = [[mappingResult.dictionary objectForKey:@""] objectForKey:@"notificationId"];
NSSring *notificationIDString = [notificationID stringValue];