如何配置RestKit来解析此JSON:
{"platform":["This field is required."],"push_token":["This field is required."]}
RestKit可以在此JSON中支持不同的各种字段集吗?如示例所示,只有一个字段“platform”或一个字段“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/}
最佳答案
我刚刚检查了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
关于ios - 由RestKit解析JSON与单个字符串元素的列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32822098/