映射嵌套对象和数组

时间:2015-04-22 19:04:07

标签: objective-c json restkit-0.20

大家下午好!

我试图弄清楚如何使用嵌套数组映射对象,但是由于未捕获的异常,我的项目仍然被终止。我假设我没有正确映射某些内容,但我被告知某些内容不符合键值编码。

如何使用嵌套数组映射对象?

以下是我试图映射的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 &amp; APIManager.m

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

迈克尔

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您从description映射到description,但该属性的标识符为productDescription。更改映射或属性名称。