2015-09-28 104 views
2

單字符串元素的列表如何配置RestKit解析此JSON:JSON解析與RestKit

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

可以在此JSON的RestKit支持不同的各套場的?如示例所示,只有一個字段「平臺」或一個字段「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/} 
+0

不清楚你在問什麼,告訴你正在試圖映射到類定義。 – Wain

+0

@完成了。但是這段代碼不起作用:) – ftp27

+0

在這裏感到困惑,你是說上面的代碼不能解析你上面提到的json嗎?或者你只是問上面的代碼是否適合你的目的? –

回答

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