JSON 格式不一致 [英] JSON format inconsistency
问题描述
我遇到过两种 JSON 格式:
格式 A:
<预><代码>[{话题":{"category":"testCategory","created_at":"2011-09-27T05:41:42Z",大小":5,标题":testTitle2",id":1,帖子":[{"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",大小":5,标题":testTitle2",id":1,帖子":[{"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 时,我收到以下错误:
... 找不到 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"];//设置我们的对象映射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]];句子Mapping.primaryKeyAttribute = @"postID";[sentencesMapping mapKeyPathsToAttributes:@"id", @"postID",...@"text", @"text", nil];//设置关系[storyMapping mapRelationship:@"posts" withMapping:postMapping];//话题->(帖子)->邮政//向提供者注册我们的映射[objectManager.mappingProvider setMapping:topicMapping forKeyPath:@"topic"];[objectManager.mappingProvider setMapping:postMapping forKeyPath:@"post"];//设置路由器[objectManager.router routeClass:[Topic class] toResourcePath:@"/topics.json" forMethod:RKRequestMethodPOST];[objectManager.router routeClass:[主题类] 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:[主题类]];[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];}];}
如果有人感兴趣,我解决了:
在 rails 中,我更改了模型,因此它将像这样发送 restKit 客户端嵌套的 json(格式 A):
class 主题 <ActiveRecord::Basehas_many : 帖子accepts_nested_attributes_for :postsself.include_root_in_json = true结尾
我将 post 方法属性嵌套如下:
-(void)sendTopic{RKObjectManager *manager =[RKObjectManager sharedManager];[manager postObject:self.topic delegate:nil block:^(RKObjectLoader *loader) {RKObjectMapping* sendTopicMapping = [RKManagedObjectMapping mappingForClass:[主题类]];[sendTopicMapping mapKeyPathsToAttributes:@"topic[id]", @"topicID",....@"topic[title]", @"title", nil];RKManagedObjectMapping* postPostMapping = [RKManagedObjectMapping mappingForClass:[Post class]];[postPostMapping mapKeyPathsToAttributes:@"post[id]", @"postID",....@"post[text]", @"text", nil];[sendTopicMapping mapRelationship:@"posts" withMapping:postPostMapping];[manager.mappingProvider setMapping:sendTopicMapping forKeyPath:@"topic"];loader.serializationMapping = [sendTopicMapping inverseMapping];}];}
There are two JSON format that i came across:
Format 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}
]
}
}
]
Format 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}
]
}
]
When my restKit client gets format B she is pleased but when she gets format A i get the following error:
... Could not find an object mapping for keyPath: '' ...
My restKit configurations is at the bottom.
So i fixed it by making my rails 3.1 server send her a format B JSON.
But now, restKit sends the server a format A JSON and he is stump as he should be.
Why does restKit won't receive format A but sends formats A? Is there a way to make restKit send a format B JSON? Or, maybe is there a way to make rails send format B and receive format A?
my restKit configurations:
-(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];
}];
}
I solved it if anyone is interested:
in rails i changed the modle so it will send the restKit client nested json(Format A) like this:
class Topic < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
self.include_root_in_json = true
end
I made the post method attributes nested like this:
-(void)sendTopic{
RKObjectManager *manager =[RKObjectManager sharedManager];
[manager postObject:self.topic delegate:nil block:^(RKObjectLoader *loader) {
RKObjectMapping* sendTopicMapping = [RKManagedObjectMapping mappingForClass:[Topic class]];
[sendTopicMapping mapKeyPathsToAttributes:
@"topic[id]", @"topicID",
....
@"topic[title]", @"title", nil];
RKManagedObjectMapping* postPostMapping = [RKManagedObjectMapping mappingForClass:[Post class]];
[postPostMapping mapKeyPathsToAttributes:
@"post[id]", @"postID",
....
@"post[text]", @"text" , nil];
[sendTopicMapping mapRelationship:@"posts" withMapping:postPostMapping];
[manager.mappingProvider setMapping:sendTopicMapping forKeyPath:@"topic"];
loader.serializationMapping = [sendTopicMapping inverseMapping];
}];
}
这篇关于JSON 格式不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!