2015-04-22 282 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尋找解決方案,但如果我無法獲得任何工作,我會考慮您提到的項目。謝謝! –