RESTful 服务响应未映射到对象 - RESTKit
RESTful service response isn't mapping to an object - RESTKit
我有一个来自 REST 服务的 JSON 响应我正在使用 RESTKit 但它没有被映射,下面是相同的来源
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"strCode" toKeyPath:@"UserCode"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping
objectClass:[User class]
rootKeyPath:nil method:RKRequestMethodPOST];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
[responseMapping addAttributeMappingsFromDictionary:@{ @"Id":@"Id",@"UserCode":@"strCode",@"FirstName": @"strFname", @"LastName": @"strLname",@"Email":@"strEmail",@"PhoneNumber":@"strPhoneNumber",@"CompanyName":@"strCompany",@"Address":@"strAddress",@"Abn":@"strAbn"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
RKObjectMapping *errResponseMapping = [RKObjectMapping mappingForClass:[ServiceError class]];
[errResponseMapping addAttributeMappingsFromDictionary:@{ @"ErrorMessage": @"strErrorMessage", @"FriendlyMessage": @"strFriendlyMessage"}];
RKResponseDescriptor *errResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errResponseMapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[manager addResponseDescriptor:errResponseDescriptor];
manager.requestSerializationMIMEType = RKMIMETypeJSON;
User *user = [user new];
user.strCode = txtCode.text;
// POST the parameterized representation of the `page` object to `/posts` and map the response
[manager postObject:user path:[ServiceUrls userDetails] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSlog(@"%d",result.count);
}
} failure:nil];
用户class看起来像这样
@interface User : NSObject
{
}
@property (nonatomic,retain) NSNumber *Id;
@property (nonatomic,retain) NSString *strCode;
@property (nonatomic,retain) NSString *strFname;
@property (nonatomic,retain) NSString *strLname;
@property (nonatomic,retain) NSString *strEmail;
@property (nonatomic,retain) NSString *strPhoneNum;
@property (nonatomic,retain) NSString *strCompany;
@property (nonatomic,retain) NSString *strAddress;
@property (nonatomic,retain) NSString *strAbn;
@end
JSON 我得到但未映射的响应如下
{"Id":7,
"UserCode":"CC1234",
"FirstName":"Test name_",
"LastName":"Banga",
"Email":"p@b.com",
"PhoneNumber":"0421196587",
"CompanyName":"String",
"Address":"String",
"Abn":"String"}
不确定我添加的代码有什么问题,有什么线索吗?
也许试试这个。
映射:
-(void) getUserMapping {
RKEntityMapping *userEntityMapping = [self generateEntityMappingFromClass:[User class]];
userEntityMapping.identificationAttributes = @[@"Id"];
return userEntityMapping;
}
生成映射:
+ (RKEntityMapping *)generateEntityMappingFromClass:(Class)objectClass {
NSAssert(objectClass != NULL, @"Cannot generate a mapping from a null object class.");
NSDictionary *propertiesAndClasses = [[RKPropertyInspector sharedInspector] propertyInspectionForClass:objectClass];
NSAssert([propertiesAndClasses count] > 0, @"Cannot generate a mapping from a class containing zero properties.");
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass(objectClass) inManagedObjectStore: [RKManagedObjectStore defaultStore]];
NSMutableDictionary *mappingDictionary = [[NSMutableDictionary alloc] init];
for (NSString *property in [propertiesAndClasses allKeys]) {
NSLog(@"property: %@", property);
[mappingDictionary setObject:property forKey:property];
}
[mapping addAttributeMappingsFromDictionary:mappingDictionary];
return mapping;
}
响应描述符:
RKResponseDescriptor *responseDescriptorBody = [RKResponseDescriptor responseDescriptorWithMapping:[self getUserMapping] method:RKRequestMethodGET pathPattern:@"" keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
我认为你的问题是 JSON 没有 keyPath,请尝试将其从 nil 更改为 @""
我有一个来自 REST 服务的 JSON 响应我正在使用 RESTKit 但它没有被映射,下面是相同的来源
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"strCode" toKeyPath:@"UserCode"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping
objectClass:[User class]
rootKeyPath:nil method:RKRequestMethodPOST];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
[responseMapping addAttributeMappingsFromDictionary:@{ @"Id":@"Id",@"UserCode":@"strCode",@"FirstName": @"strFname", @"LastName": @"strLname",@"Email":@"strEmail",@"PhoneNumber":@"strPhoneNumber",@"CompanyName":@"strCompany",@"Address":@"strAddress",@"Abn":@"strAbn"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
RKObjectMapping *errResponseMapping = [RKObjectMapping mappingForClass:[ServiceError class]];
[errResponseMapping addAttributeMappingsFromDictionary:@{ @"ErrorMessage": @"strErrorMessage", @"FriendlyMessage": @"strFriendlyMessage"}];
RKResponseDescriptor *errResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errResponseMapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[manager addResponseDescriptor:errResponseDescriptor];
manager.requestSerializationMIMEType = RKMIMETypeJSON;
User *user = [user new];
user.strCode = txtCode.text;
// POST the parameterized representation of the `page` object to `/posts` and map the response
[manager postObject:user path:[ServiceUrls userDetails] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSlog(@"%d",result.count);
}
} failure:nil];
用户class看起来像这样
@interface User : NSObject
{
}
@property (nonatomic,retain) NSNumber *Id;
@property (nonatomic,retain) NSString *strCode;
@property (nonatomic,retain) NSString *strFname;
@property (nonatomic,retain) NSString *strLname;
@property (nonatomic,retain) NSString *strEmail;
@property (nonatomic,retain) NSString *strPhoneNum;
@property (nonatomic,retain) NSString *strCompany;
@property (nonatomic,retain) NSString *strAddress;
@property (nonatomic,retain) NSString *strAbn;
@end
JSON 我得到但未映射的响应如下
{"Id":7,
"UserCode":"CC1234",
"FirstName":"Test name_",
"LastName":"Banga",
"Email":"p@b.com",
"PhoneNumber":"0421196587",
"CompanyName":"String",
"Address":"String",
"Abn":"String"}
不确定我添加的代码有什么问题,有什么线索吗?
也许试试这个。 映射:
-(void) getUserMapping {
RKEntityMapping *userEntityMapping = [self generateEntityMappingFromClass:[User class]];
userEntityMapping.identificationAttributes = @[@"Id"];
return userEntityMapping;
}
生成映射:
+ (RKEntityMapping *)generateEntityMappingFromClass:(Class)objectClass {
NSAssert(objectClass != NULL, @"Cannot generate a mapping from a null object class.");
NSDictionary *propertiesAndClasses = [[RKPropertyInspector sharedInspector] propertyInspectionForClass:objectClass];
NSAssert([propertiesAndClasses count] > 0, @"Cannot generate a mapping from a class containing zero properties.");
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass(objectClass) inManagedObjectStore: [RKManagedObjectStore defaultStore]];
NSMutableDictionary *mappingDictionary = [[NSMutableDictionary alloc] init];
for (NSString *property in [propertiesAndClasses allKeys]) {
NSLog(@"property: %@", property);
[mappingDictionary setObject:property forKey:property];
}
[mapping addAttributeMappingsFromDictionary:mappingDictionary];
return mapping;
}
响应描述符:
RKResponseDescriptor *responseDescriptorBody = [RKResponseDescriptor responseDescriptorWithMapping:[self getUserMapping] method:RKRequestMethodGET pathPattern:@"" keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
我认为你的问题是 JSON 没有 keyPath,请尝试将其从 nil 更改为 @""