2015-04-22 286 views
0

大家下午好!映射嵌套对象和数组

我想弄清楚如何映射对象与嵌套数组,但我的项目由于未捕获的异常不断终止。我假设我没有正确地映射某些东西,但我被告知某些东西不符合键值编码。

如何映射一个嵌套数组的对象?

以下是我试图映射的JSON的接口,接口和实现以及正在抛出的错误。最后,在我的GitHub项目上有一个链接,因为我遗漏了任何东西,或者看到完整的源代码会有所帮助。

JSON
{ 
    "href": "string", 
    "items": [ 
    { 
     "type": "string", 
     "status": "string", 
     "name": "string", 
     "publisher": "string", 
     "publisherId": "string", 
     "description": "string", 
     "url": "string", 
     "smallLogoImageUrl": "string", 
     "tileImageUrl": "string", 
     "heroImageUrl": "string", 
     "tags": [ 
     "string", 
     "string" 
     ], 
     "createdOn": "2015-04-22T18:55:40.782Z", 
     "downloadUrl": "string", 
     "getProductCodeUrl": "string", 
     "metadata": { 
     "exeType": "string", 
     "packageFileName": "string", 
     "installDirectory": "string", 
     "executableName": "string" 
     }, 
     "id": "string" 
    } 
    ] 
} 

接口(.H)

@interface SFrontPageItem : NSObject 

@property (nonatomic, strong) NSString *type; 
@property (nonatomic, strong) NSString *status; 
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *publisher; 
@property (nonatomic, strong) NSString *publisherId; 
@property (nonatomic, strong) NSString *productDescription; 
@property (nonatomic, strong) NSString *url; 
@property (nonatomic, strong) NSString *smallLogoImageUrl; 
@property (nonatomic, strong) NSString *tileImageUrl; 
@property (nonatomic, strong) NSString *heroImageUrl; 
@property (nonatomic, strong) NSArray *tags; 
@property (nonatomic, strong) NSString *createdOn; 
@property (nonatomic, strong) NSString *downloadUrl; 
@property (nonatomic, strong) NSString *getProductCodeUrl; 
@property (nonatomic, strong) NSArray *metadata; 
@property (nonatomic, strong) NSString *productID; 

@end 


@interface SFrontPage : NSObject 

@property (nonatomic, strong) NSString *href; 
@property (nonatomic, strong) NSArray *items; 

@end 

实现(.M)

- (void) getFrontPage 
{ 

    AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate]; 

    RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[SFrontPageItem class]]; 

    [itemMapping addAttributeMappingsFromDictionary:@{ 
                 @"type": @"type", 
                 @"status": @"status", 
                 @"name": @"name", 
                 @"publisher": @"publisher", 
                 //@"publisherId": @"publisherId", 
                 @"description": @"description", 
                 @"url": @"url", 
                 //@"smallLogoImageUrl": @"smallLogoImageUrl", 
                 @"tileImageUrl": @"tileImageUrl", 
                 //@"heroImageUrl": @"heroImageUrl", 
                 //@"tags": @"tags", 
                 @"createdOn": @"createdOn", 
                 //@"downloadUrl": @"downloadUrl", 
                 //@"getProductCodeUrl": @"getProductCodeUrl", 
                 //@"metadata": @"metadata", 
                 @"id": @"productID" 
                }]; 
    //itemMapping.forceCollectionMapping = YES; 


    RKObjectMapping *frontpageMapping = [RKObjectMapping mappingForClass:[SFrontPage class]]; 
    [frontpageMapping addAttributeMappingsFromDictionary:@{ 
                  @"href": @"href" 
                 }]; 


    [frontpageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"items" 
                        toKeyPath:@"items" 
                        withMapping:itemMapping]]; 


    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:frontpageMapping 
                          method:RKRequestMethodGET 
                         pathPattern:nil 
                          keyPath:nil 
                         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

    [self.objectManager.HTTPClient setAuthorizationHeaderWithUsername:self.sconnection.apiKey.key password:self.sconnection.apiKey.secret]; 
    [self.objectManager addResponseDescriptor:responseDescriptor]; 
    [self.objectManager getObjectsAtPath:@"/api/frontpage/rest" parameters:nil 
     success:^(RKObjectRequestOperation *operation, RKMappingResult *result) 
     { 

      SFrontPage *newFrontpage = result.firstObject; 
      NSLog (@" HREF: %@", newFrontpage.href); 

      //NSLog (@"ITEMS: %@", newFrontpage.items.firstObject); 
      //SFrontPageItem *newFrontpageItem = newFrontpage.items.firstObject; 
      //NSLog (@"Unexpected Great Thing %@", newFrontpageItem); 

      [appDelegate.loginViewController apiConnectionSuccess]; 


     } failure:^(RKObjectRequestOperation *operation, NSError *error) 
     { 

      [appDelegate.loginViewController updateLoginWindowHeaderLabelTo:@"Unable to Load Frontpage"]; 
      [appDelegate.loginViewController apiConnectionFailure]; 
     }]; 
} 

错误
[<SFrontPageItem 0x6180001026d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key description. 

来源

完整的源代码可以发现on GitHub,有关文件是APIManager.h & APIManager.m

我希望我已经够清楚了,当我形成一个关于我不完全理解的东西的问题时,我有时会错过标记。我对ObjC和RestKit都很陌生,所以我确信在我的代码中已经有很多令人困惑的事情。感谢您花时间阅读它并考虑我的问题。如果我能澄清任何事情,请告诉我!

Michael

回答

1

您从映射到descriptiondescription,但酒店标识productDescription。更改映射或属性名称。

+0

我没有听到,谢谢!我有点傻眼,这是整个问题。看看双重检查这些事情是多么重要。我会尽快接受你的回答。 –

+1

在这些错误上,人们可以度过他的一生。我从错误信息中得到它说,它找不到'description'。好吧,我也是。 ;-) –

1

尝试使用一些现成的解决方案。 https://github.com/aryaxt/OCMapper https://github.com/isair/JSONHelper

+0

谢谢你的建议,@Gal! RestKit似乎用于对象映射,我想避免使用太多的第三方扩展。如果可能的话,我宁愿为RestKit寻找解决方案,但如果我无法获得任何工作,我会考虑您提到的项目。谢谢! –