映射嵌套对象和数组
Mapping nested objects and arrays
大家下午好!
我正在尝试弄清楚如何使用嵌套数组映射对象,但由于未捕获的异常,我的项目一直被终止。我假设我没有正确映射某些东西,但我被告知某些东西不符合键值编码。
如何使用嵌套数组映射对象?
以下是我尝试映射的 JSON 的足迹、接口和实现,以及抛出的错误。最后,我在 GitHub 上的项目有一个 link,以防我遗漏任何内容,或者查看完整的源代码会有所帮助。
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 都是新手,所以我确信我的代码中已经有很多令人困惑的东西。感谢您花时间阅读它,并考虑我的问题。如果我能澄清任何事情,请告诉我!
迈克尔
尝试使用一些现成的解决方案。
https://github.com/aryaxt/OCMapper
https://github.com/isair/JSONHelper
您从 description
映射到 description
,但 属性 具有标识符 productDescription
。更改映射或 属性 名称。
大家下午好!
我正在尝试弄清楚如何使用嵌套数组映射对象,但由于未捕获的异常,我的项目一直被终止。我假设我没有正确映射某些东西,但我被告知某些东西不符合键值编码。
如何使用嵌套数组映射对象?
以下是我尝试映射的 JSON 的足迹、接口和实现,以及抛出的错误。最后,我在 GitHub 上的项目有一个 link,以防我遗漏任何内容,或者查看完整的源代码会有所帮助。
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 都是新手,所以我确信我的代码中已经有很多令人困惑的东西。感谢您花时间阅读它,并考虑我的问题。如果我能澄清任何事情,请告诉我!
迈克尔
尝试使用一些现成的解决方案。 https://github.com/aryaxt/OCMapper https://github.com/isair/JSONHelper
您从 description
映射到 description
,但 属性 具有标识符 productDescription
。更改映射或 属性 名称。