Restkit:如何从网站获取这些JSON值

时间:2013-04-04 19:05:18

标签: iphone ios objective-c xcode restkit

我是Restkit的新手,基本上需要从nsurl网站moo / api.com(例如nsurl)中检索这些值。什么是最好的方法。已经声明了属性并进行了对象映射。现在需要帮助返回nslog中的值。该类是视图控制器。任何帮助将不胜感激。

.h

@property (nonatomic, copy) NSString* ID;
@property (nonatomic, copy) NSString* Text;
@property (nonatomic, copy) NSString* author;

.m

- (void)viewDidLoad
{
    RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Terms class]];
    [articleMapping addAttributeMappingsFromDictionary:@{
        @"ID": @"ID",
        @"Text": @"Text",
        @"author": @"author"
     }];
 }

1 个答案:

答案 0 :(得分:0)

有关RestKit的介绍,请关注this link to the official wiki。有几篇文章涉及Object Mapping,发布/检索数据等等。

以下示例取自上述维基:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping pathPattern:nil keyPath:@"__YOUR_KEYPATH__" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

NSURL *URL = [NSURL URLWithString:@"__YOUR_URL__"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];

[objectRequestOperation start];