我在IOS上使用restkit。我在解析一个简单的json对象时遇到了麻烦。
我有一个简单的json对象返回:
{"_id"=>"537c235189d50fabcc000009",
"about"=>"nice",
"headline"=>"looking",
"access_token"=>
"$2a$10$oZ4IiaVBxHhc1qGeBoZv1uYonBM3Qb5Y010rTkUOynDZIGdGagqJy"}
我的设置:
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureRestKit];
[self loadVenues];
}
- (void)loadVenues
{
NSString *clientToken = SNAPTOKEN;
NSDictionary *profile = @{@"os": @"iOS",
@"device_token": @"first_token",
@"password":@"password",
@"email":@"email",
@"headline":@"hello world"};
NSDictionary *queryParams = @{@"token" : clientToken,
@"profile" : profile};
[[RKObjectManager sharedManager] postObject: nil
path: @"/profiles/new"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"log%@", mappingResult );
_profile = mappingResult.array;
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"What do you mean by 'there is no coffee?': %@", error);
}];
}
- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://airimg.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Profile class]];
[venueMapping addAttributeMappingsFromDictionary:@{ @"_id": @"about", @"access_token": @"headline" }];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:venueMapping
method:RKRequestMethodPOST
pathPattern:@"/profiles/new"
keyPath:@"response"
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
我有以下错误:
NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: response
The representation inputted to the mapper was found to contain nested object representations at the following key paths: _id, about, access_token, headline
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}
答案 0 :(得分:0)
主要问题是您的响应描述符使用response
的密钥路径,而您的JSON不会在顶层包含该密钥。因此,将关键路径设置为“nil'。
此外,您的视图控制器应存储对objectManager
的引用,并在以后再次使用它。目前您使用[RKObjectManager sharedManager]
并返回曾经创建的第一个对象管理器,因此只要您有多个视图控制器,每个视图控制器都创建自己的对象管理器,您将遇到问题。
当然,视图控制器不一定要创建单独的对象管理器......