RestKit:如何在没有任何嵌套属性的情况下将NSManagedObject作为JSON发布?

时间:2013-11-18 04:42:49

标签: ios iphone objective-c restkit restkit-0.20

这应该是一个非常简单的,但遗憾的是我没有找到任何答案......

RestKit将我的对象映射到:

request.body={"user":{"pass":"1234","id":0,"login":"awesome_guy","tier":0}}

我真正想要的是什么:

{"pass":"1234","id":0,"login":"awesome_guy","tier":0}

没有对象名称“user”。

如果您已经处理过此问题,则需要5秒钟才能回答。如果你还没有使用过RestKit。你不知道答案。无论如何我附上了我的代码:

用户对象映射:

/* ===========================
 * ====User Object Mapping====
 * ==========================*/

RKEntityMapping* userObjectMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([User class]) inManagedObjectStore:objectManager.managedObjectStore];

NSDictionary *userObjectMappingDict = @{
                                    @"id":@"id",
                                    @"login":@"login",
                                    @"firstName":@"firstName",
                                    @"lastName":@"lastName",
                                    @"phoneNumber":@"phoneNumber",
                                    @"email":@"email",
                                    @"tier":@"tier",
                                    @"sessionId":@"sessionId",
                                    @"pass":@"password"
                                    };

userObjectMapping.identificationAttributes = @[@"id"];

[userObjectMapping addAttributeMappingsFromDictionary:userObjectMappingDict];

RKEntityMapping* userBusinessMapping =  [RKEntityMapping mappingForEntityForName:@"Business" inManagedObjectStore:objectManager.managedObjectStore];
[userBusinessMapping addAttributeMappingsFromDictionary:@{@"business":@"id"}]; // Nil Key path

[userObjectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"business" withMapping:userBusinessMapping]];

1 个答案:

答案 0 :(得分:1)

您应该有一个未显示的RKRequestDescriptor实例。它是使用requestDescriptorWithMapping:objectClass:rootKeyPath:创建的。您将根密钥路径设置为@"user",并且应将其设置为nil。