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/}
不清楚你在問什麼,告訴你正在試圖映射到類定義。 – Wain
@完成了。但是這段代碼不起作用:) – ftp27
在這裏感到困惑,你是說上面的代碼不能解析你上面提到的json嗎?或者你只是問上面的代碼是否適合你的目的? –