我正在尝试使用 Restkit 对象映射发布实体(文章)及其关系(作者)的内容。
文章 <->> 作者 一篇文章可以有多个作者。
我当前的对象映射如下,
RKObjectMapping *authorMapping = [RKObjectMapping requestMapping];
[authorMapping addAttributeMappingsFromArray:@[@"name",@"email"]];
RKObjectMapping *articleMapping = [RKObjectMapping requestMapping];
[articleMapping addAttributeMappingsFromArray:@[@"title",@"body",@"date"]];
RKRelationshipMapping * rel =[RKRelationshipMapping relationshipMappingFromKeyPath:@"author" toKeyPath:@"author" withMapping:authorMapping];
[articleMapping addPropertyMapping:rel];
RKObjectMapping *mapping = [RKObjectMapping requestMapping];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"singleArticle" toKeyPath:@"singleArticle" withMapping:articleMapping]];
对于这个映射,我期待一个带有一组作者的 JSON 输出,如下所示,
{
"singleArticle": {
"author": [
{
"email": "dadsa@",
"name": "rk"
},
{
"email": "3ldll",
"name": "rjk"
}
],
"body": "body content",
"date": "2014-05-16T15:54:40Z",
"title": "some title"
}
}
但是生成的 JSON 输出结果将作者类中的所有字段作为一个单独的对象,如下所示,
{
"singleArticle": {
"author": [
{
"email": "dadsa@"
},
{
"name": "rk"
},
{
"email": "3ldll"
},
{
"name": "rjk"
}
],
"body": "body content",
"date": "2014-05-16T15:54:40Z",
"title": "some title"
}
}
我不确定我在这里遗漏了什么会弄乱上面的 JSON 提要。有什么想法可以使用一组作者对象生成正确的 JSON 吗?