【问题标题】:Parsing a simple JSON object with restkit使用 restkit 解析一个简单的 JSON 对象
【发布时间】:2014-07-09 13:22:24
【问题描述】:

我在 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}

【问题讨论】:

    标签: ios parsing mapping restkit


    【解决方案1】:

    主要问题是您的响应描述符使用 response 的键路径,而您的 JSON 不包含顶层的该键。因此,将密钥路径设置为“nil”。

    另外,您的视图控制器应该存储对objectManager 的引用并稍后再次使用它。目前,您使用 [RKObjectManager sharedManager] 并返回曾经创建的 first 对象管理器,因此一旦您有多个视图控制器,每个视图控制器都创建自己的对象管理器,您就会遇到问题。

    当然,视图控制器不一定要创建单独的对象管理器...

    【讨论】:

    • 谢谢!你有任何设计文章要指出吗?我对来自红宝石世界的 IOS 开发非常满意。这基本上是我的第一个概念证明,我可以从设备发布。
    • 没有我可以指出的文章,但它是关于实例重用的 - 通常你所有的视图控制器都将与相同的 Web 服务和数据模型进行交互(实际上,视图控制器可能应该被抽象掉来自某些数据控制器的那种事情......)
    • 太棒了。而且因为我是全新的和无耻的,数据控制器会继承自 NSObject 吗?对吗?
    • 是的,通常会。它应该只有一个目的,所以不同的父母没有多大意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2021-08-24
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多