【发布时间】:2015-09-28 11:40:03
【问题描述】:
如何配置 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/}
【问题讨论】:
-
不清楚你在问什么,显示你试图映射到的类定义。
-
@Wain ,完成。但是这段代码不起作用:)
-
这里一头雾水,你的意思是上面的代码不能解析你上面提到的json吗?或者您只是在问上面的代码是否适合您的目的?
-
@ybbaigo 该代码不适用于上面介绍的 json
-
restkit 的错误信息是什么?