我遇到了两种 JSON 格式:
格式 A:
[
{"topic":
{"category":"testCategory","created_at":"2011-09-27T05:41:42Z",
"size":5,"title":"testTitle2", "id":1,
"posts":[
{"number":1,"created_at":"2011-09-27T05:41:42Z",
"text":"text2","id":1,"topic_id":1},
{"number":0,"created_at":"2011-09-27T05:41:42Z",
"text":"sentence1","id":2,"story_id":1}
]
}
}
]
格式 B:
[
{"category":"testCategory","created_at":"2011-09-27T05:41:42Z",
"size":5,"title":"testTitle2", "id":1,
"posts":[
{"number":1,"created_at":"2011-09-27T05:41:42Z",
"text":"text2","id":1,"topic_id":1},
{"number":0,"created_at":"2011-09-27T05:41:42Z",
"text":"sentence1","id":2,"story_id":1}
]
}
]
当我的 restKit 客户端获得格式 B 时,她很高兴,但是当她获得格式 A 时,我收到以下错误:
... Could not find an object mapping for keyPath: '' ...
我的 restKit 配置在底部。
所以我通过让我的 rails 3.1 服务器向她发送格式 B JSON 来修复它。
但是现在,restKit 向服务器发送格式 A JSON 并且他应该是树桩。
为什么restKit不会接收格式A而是发送格式A?有没有办法让restKit发送格式B JSON?或者,也许有办法让 Rails 发送格式 B 并接收格式 A?
我的 restKit 配置:
-(void)initRestKit{
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000/"];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"WTF.sqlite"];
// Setup our object mappings
RKManagedObjectMapping* topicMapping = [RKManagedObjectMapping mappingForClass:[Topic class]];
storyMapping.primaryKeyAttribute = @"topicID";
storyMapping.setDefaultValueForMissingAttributes = YES;
[storyMapping mapKeyPathsToAttributes:
@"id", @"topicID",
....
@"created_at", @"dateCreated", nil];
RKManagedObjectMapping* postMapping = [RKManagedObjectMapping mappingForClass:[Post class]];
sentencesMapping.primaryKeyAttribute = @"postID";
[sentencesMapping mapKeyPathsToAttributes:
@"id", @"postID",
...
@"text", @"text" , nil];
//setup relationships
[storyMapping mapRelationship:@"posts" withMapping:postMapping];//topic -> (posts) -> post
// Register our mappings with the provider
[objectManager.mappingProvider setMapping:topicMapping forKeyPath:@"topic"];
[objectManager.mappingProvider setMapping:postMapping forKeyPath:@"post"];
// Set Up Router
[objectManager.router routeClass:[Topic class] toResourcePath:@"/topics.json" forMethod:RKRequestMethodPOST];
[objectManager.router routeClass:[Topic class] toResourcePath:@"/topics/(topicID).json"];
[objectManager.router routeClass:[Post class] toResourcePath:@"/topics/(topic.topicID)/posts.json" forMethod:RKRequestMethodPOST];
[objectManager.router routeClass:[Post class] toResourcePath:@"/topics/(topic.topicID)/(post.postID).json"];
}
-(void)sendTopic{
RKObjectManager *manager =[RKObjectManager sharedManager];
[manager postObject:self.topic delegate:nil block:^(RKObjectLoader *loader) {
RKObjectMapping* sendTopicMapping = [RKManagedObjectMapping mappingForClass:[Topic class]];
[sendTopicMapping mapKeyPathsToAttributes:
@"id", @"topicID",
....
@"title", @"title", nil];
RKManagedObjectMapping* postPostMapping = [RKManagedObjectMapping mappingForClass:[Post class]];
[postPostMapping mapKeyPathsToAttributes:
@"id", @"postID",
....
@"text", @"text" , nil];
[sendTopicMapping mapRelationship:@"posts" withMapping:postPostMapping];
[manager.mappingProvider setMapping:sendTopicMapping forKeyPath:@"topic"];
loader.serializationMapping = [sendTopicMapping inverseMapping];
}];
}