所以我尝试使用RestKit从我的rails应用程序中获取JSON中的信息
我这样做的代码是这样的:
应用代表
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Initialise RestKit
NSURL *URL = [NSURL URLWithString:@"https://myapp.dev"];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:URL];
//Enable Activity Indicator Spinner
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Info class]];
[infoMapping addAttributeMappingsFromDictionary:@{
@"sample":@"sample",
@"sample_1":@"sample_1"
}];
RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
pathPattern:@"/info"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:infoDescriptor];
}
查看文件
- (void)loadInfo
{
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager getObjectsAtPath:@"/info"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSArray *info = [mappingResult array];
NSLog(@"Loaded info: %@", info);
_info = info;
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting into"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}];
}
问题是。在日志输出中,RestKit告诉我它成功映射了所有内容。但是当我尝试使用登录视图文件的方法和使用po
的调试器查看对象时,我得到以下内容
374 Finished performing object mapping. Results: {
"<null>" = "<Info: 0xa291b30>";
}
我无法查看对象,并显示断点:
我这几天一直在努力,我不知道还有什么可以尝试的。任何帮助将不胜感激
答案 0 :(得分:2)
我遇到了类似的问题。我不太清楚为什么,但是当我做了以下修改它时。
- (void)loadInfo
{
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager getObjectsAtPath:@"/info"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSArray *info = [mappingResult array];
NSLog(@"Loaded info: %@", info);
_info = info;
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting into"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Initialise RestKit
NSURL *URL = [NSURL URLWithString:@"https://myapp.dev"];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:URL];
//Enable Activity Indicator Spinner
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Info class]];
[infoMapping addAttributeMappingsFromDictionary:@{
@"sample":@"sample",
@"sample_1":@"sample_1"
}];
RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
pathPattern:@"/info/:id"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:infoDescriptor];
}
答案 1 :(得分:1)
当您加载没有嵌套keyPath的对象表示时,RestKit将映射的对象存储在字典中的[NSNull null]
键下(因为nil
不是有效的字典键)。您可以通过调用firstObject
对象上的array
,dictionary
或RKMappingResult
来检索映射结果,以访问映射的对象。
我看到一个关于将数组映射到单个对象的后续问题...您的JSON看起来是什么样的,您是如何表示它的?
答案 2 :(得分:-1)
RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];