我想使用restkit将以下信息发布到服务器:
[
{ "title": "cvxcv" },
{ "title": "sdfsdf" },
{ "title": "qsfqsdqsd" }
]
我定义了以下两个类:
@interface ObjectTemplate : NSObject
@property (nonatomic, copy) NSString *title;
@end
@interface ObjectListTemplate : NSObject
@property (nonatomic, strong) NSArray *objects;
@end
以下是他们的映射:
// Object template
RKObjectMapping *objectTemplateMapping = [RKObjectMapping mappingForClass:[ObjectTemplate class]];
[objectTemplateMapping mapAttributes:@"title", nil];
[_objectManager.mappingProvider setSerializationMapping:[objectTemplateMapping inverseMapping] forClass:[ObjectTemplate class]];
// Object list template
RKObjectMapping *objectListTemplateMapping = [RKObjectMapping mappingForClass:[ObjectListTemplate class]];
[objectListTemplateMapping mapKeyPath:@"objects" toRelationship:@"objects" withMapping:objectTemplateMapping];
RKObjectMapping *inverseObjectListTemplateMapping = [objectListTemplateMapping inverseMapping];
[_objectManager.mappingProvider setSerializationMapping:inverseObjectListTemplateMapping forClass:[ObjectListTemplate class]];
问题是每当发布或将objectListTemplate发布到服务器时,我都会获得一个根节点:
HTTP正文:{“对象”:[{“title”:“TestTitle”}}}
服务器无法识别此信息。我需要能够获得以下格式:
HTTP正文:“对象”:[{“title”:“TestTitle”}]
请帮忙,过去两天这让我疯了。
答案 0 :(得分:0)
您应该可以省略ObjectListTemplate
,发布NSArray并仅定义内部映射。 RESTkit应该意识到它是一个数组,并将其对象映射到定义的映射,这应该以你想要的方式结束。