使用RestKit解析带有单个字符串元素列表的JSON

时间:2015-09-28 11:40:04

标签: ios json rest restkit

如何配置RestKit以解析此JSON:

{"platform":["This field is required."],"push_token":["This field is required."]}

RestKit可以在这个JSON中支持不同的字段集吗?只有一个领域"平台"或者一个字段" push_token"或两个字段,如示例所示。

UPD:

@interface Register : NSObject

@property (nonatomic, copy) NSString * platform;
@property (nonatomic, copy) NSString * push_token;

+ (void) initEntityForRestKit;
+ (NSString *) getURL;
+ (NSDictionary*) elementToPropertyMappings;
+ (RKObjectMapping *) entityMapping;

@end

@implementation Register

+ (NSString *) getURL {
    return @"/api/register/";
}

+ (void) initEntityForRestKit {
    RKResponseDescriptor *responseFaultDescriptor = [RKResponseDescriptor
                                                     responseDescriptorWithMapping:[Register entityMapping]
                                                     pathPattern:[Register getURL]
                                                     keyPath:nil
                                                     statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
    [[RKObjectManager sharedManager] addResponseDescriptor:responseFaultDescriptor];
}

+ (NSDictionary*)elementToPropertyMappings {
    return @{@"platform":@"platform",
             @"push_token":@"push_token"};
}

+ (RKObjectMapping *) entityMapping {
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Register class]];
    [mapping addAttributeMappingsFromDictionary:[Register elementToPropertyMappings]];

    return mapping;
}

@end

UPD2:

2015-09-30 17:22:18.756 test[50727:2976562] E restkit.network:RKObjectRequestOperation.m:215 POST 'http://test.com/api/register/' (400 Bad Request / 0 objects) [request=0.2629s mapping=0.0000s total=0.4324s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Loaded an unprocessable error response (400)" UserInfo=0x7fad39651530 {NSLocalizedDescription=Loaded an unprocessable error response (400), NSErrorFailingURLKey=http://test.com/api/register/}
2015-09-30 17:22:18.756 test[50727:2976487] E app:AppDelegate.m:89 Load failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Loaded an unprocessable error response (400)" UserInfo=0x7fad39651530 {NSLocalizedDescription=Loaded an unprocessable error response (400), NSErrorFailingURLKey=http://test.com/api/register/}

1 个答案:

答案 0 :(得分:0)

我刚检查了Restkit的源代码。如果是错误响应,它将始终返回错误。您可以针对您的情况调试以下代码。

// If the response is a client error return either the mapping error or the mapped result to the caller as the error
if (isErrorStatusCode) {
    if ([self.mappingResult count] > 0) {
        error = RKErrorFromMappingResult(self.mappingResult);
    } else {
        // We encountered a client error that we could not map, throw unprocessable error
        if (! error) error = RKUnprocessableErrorFromResponse(self.response);
    }
    self.error = error;
    [self willFinish];
    return;
}

您可以在https://github.com/RestKit/RestKit/blob/development/Code/Network/RKResponseMapperOperation.m#L378

找到代码