如何将多个对象映射到一个JSON对象?
这是我的两个对象
#import <RestKit/RestKit.h>
@interface Account : NSObject
@property NSInteger id;
@property NSString* secret;
@end
@implementation Account @end
@interface Entry : NSObject
@property NSString* name;
@end
@implementation Entry @end
我希望将这些对象映射到以下JSON:
/* Goal JSON:
{
"id":10,
"secret":"supersecret",
"eventName":"Hayooo"
}
*/
首先尝试直接使用RKMapperOperation:
void testMergeMap(){
Account* account = [Account new];
account.id = 10;
account.secret = @"supersecret";
Entry* entry = [Entry new];
entry.name = @"Hayooo";
RKMapperOperation* mapper = [[RKMapperOperation alloc]
initWithRepresentation:@{@"account":account, @"entry":entry} mappingsDictionary:@{@"account.id": @"id", @"account.secret":@"secret", @"entry.name":@"eventName"}];
[mapper start];
RKMappingResult* result = mapper.mappingResult;
NSLog(@"%@", result.dictionary.description);
}
但是那次崩溃,无法真正造成错误。
那么,我应该怎么做呢?
修改
我也试过这个
RKObjectManager* testSimpleMap(Account* account, Entry* entry){
RKObjectMapping* map = [RKObjectMapping requestMapping];
[map addAttributeMappingsFromDictionary:
@{@"account.id": @"id",
@"account.secret": @"secret",
@"entry.name":@"eventName"}];
RKObjectManager* man = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://google.com"]];
RKRequestDescriptor* requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:map objectClass:nil rootKeyPath:nil method:RKRequestMethodPOST];
[man addRequestDescriptor:requestDescriptor];
[man postObject:@{@"account":account, @"entry":entry} path:@"/" parameters:nil success:nil failure:nil];
return man;
}
但是我们不能在没有类的情况下创建requestDescriptors,因此不起作用。但我认为了解我的目标是有用的。如果这样可行,那就太好了!
答案 0 :(得分:0)
mappingsDictionary:
不应该是纯字符串键和值的字典。键应该是要映射的项的关键路径,值应该是RKMapping
个实例,用于描述如何映射在关键路径中找到的数据。
一般来说,如果你刚开始我不会直接使用RKMapperOperation
。我会考虑使用RKObjectManager
。它抽象你远离这样的内在细节。
如果您想继续使用的内容,请查看示例in the docs。
答案 1 :(得分:0)
创建一个模拟关系的第三个对象,但我觉得它不够优雅。
@interface Both : NSObject
@property Account* account;
@property Entry* entry;
@end
@implementation Both @end
RKObjectManager* testRelationMap(Account* account, Entry* entry){
/* Goal JSON:
{
"user":{
"id":10,
"secret":"supersecret",
},
"entry":{
"name":"Hayooo"
}
}
*/
Both* both = [Both new];
both.account = account;
both.entry = entry;
RKObjectMapping* accountMap = [RKObjectMapping requestMapping];
[accountMap addAttributeMappingsFromArray:@[@"id", @"secret"]];
RKObjectMapping* entryMap = [RKObjectMapping requestMapping];
[entryMap addAttributeMappingsFromArray:@[@"name"]];
RKObjectMapping* relationMap = [RKObjectMapping requestMapping];
[relationMap addRelationshipMappingWithSourceKeyPath:@"account" mapping:accountMap];
[relationMap addRelationshipMappingWithSourceKeyPath:@"entry" mapping:entryMap];
RKObjectManager* man = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://google.com"]];
RKRequestDescriptor* requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:relationMap objectClass:[Both class] rootKeyPath:nil method:RKRequestMethodPOST];
[man addRequestDescriptor:requestDescriptor];
[man postObject:both path:@"/" parameters:nil success:nil failure:nil];
return man;
}
这种方式需要很多代码,但这是一个解决方案。