有我來跨越兩個JSON格式:JSON格式不一致
格式答:
[
{"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? 或者,也許有辦法讓鋼軌發送格式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];
}];
}
a和b在語義上有所不同 –