如何在通过 RESTKIT 映射之前更改 JSON
我正在使用 RESTKIT 来映射从服务器返回的 JSON。
从服务器获取的 JSON 结果如下
{"term":"Zh","results":{"users":[{"id":{"$oid":"4ebe59970a614e0019000055"},"term":"some text","score":1}]}
如何将上面的 JSON 结果转换为下面的:
{"results":{"users":[{"uid":"4ebe59970a614e0019000055","text":"some text"}]}
另外,我可以在哪里执行此操作,以便 RESTKIT 映射将使用转换后的 JSON 而不是初始的 JSON?
下面是我用来管理 JSON 和映射的加载器类
-(void)getObjects
{
RKObjectManager *sharedManager = [RKObjectManager sharedManager];
[sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(@"Loaded PAYLOAD successfully for %@, with response %@", self.resourcePath , [response bodyAsString] );
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
{
}
+ (void)setManagerAndMappings
{
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:SERVER_URL];
RKObjectMappingProvider* provider = [[RKObjectMappingProvider new] autorelease];
//User Object Mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"_id" toAttribute:@"uid"];
[userMapping mapAttributes:@"avatar_url",@"created_at",@"email",@"name",@"nickname",@"follower_count",@"following_count",@"answer_count",@"new_notification_count",@"new_questions_count",@"is_following",@"facebook_configured",@"twitter_configured",@"description",@"approved",@"type", nil];
[provider setMapping:userMapping forKeyPath:@"user"];
}
I am using RESTKIT to map the JSON returned from server.
The JSON result obtained from server is as follows
{"term":"Zh","results":{"users":[{"id":{"$oid":"4ebe59970a614e0019000055"},"term":"some text","score":1}]}
How can I convert the above JSON result to the below:
{"results":{"users":[{"uid":"4ebe59970a614e0019000055","text":"some text"}]}
Also, where can I do this so that the RESTKIT mapping will use the converted JSON instead of the initial one?
Below is the loader class that I am using to manage the JSON and mappings
-(void)getObjects
{
RKObjectManager *sharedManager = [RKObjectManager sharedManager];
[sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(@"Loaded PAYLOAD successfully for %@, with response %@", self.resourcePath , [response bodyAsString] );
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
{
}
+ (void)setManagerAndMappings
{
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:SERVER_URL];
RKObjectMappingProvider* provider = [[RKObjectMappingProvider new] autorelease];
//User Object Mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"_id" toAttribute:@"uid"];
[userMapping mapAttributes:@"avatar_url",@"created_at",@"email",@"name",@"nickname",@"follower_count",@"following_count",@"answer_count",@"new_notification_count",@"new_questions_count",@"is_following",@"facebook_configured",@"twitter_configured",@"description",@"approved",@"type", nil];
[provider setMapping:userMapping forKeyPath:@"user"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
@Shocks 的答案很吸引人,不幸的是它对于 0.20 之前的版本有效。
无论如何,类似的解决方案也适用于 0.20,使用 RKSerialization 的实现:
并
在设置过程中实现:
HTH
@Shocks answer is appealing, unfortunately it is valid a for version before 0.20.
Anyway, a similar solution is available for 0.20 too, using an implementation of RKSerialization:
and implementing
then during the setup:
HTH
RKObkectLoaderDelegate 中有一个 willMapData: 选择器 在解析完成后立即调用。 mappableData 参数是可变的,所以我想您可以在对象映射发生之前更改数据。
There is a willMapData: selector in the RKObkectLoaderDelegate that is invoked just after parsing has completed. The mappableData argumet is mutable, so i guess you can change the data just before the object mapping will take place.
使用
RKObjectRequestOperation.setWillMapDeserializedResponseBlock:
。快速:
use
RKObjectRequestOperation.setWillMapDeserializedResponseBlock:
.In swift:
对我来说,唯一的解决方案是在服务器级别修改返回的对象。
如果不能,只需返回服务器的映射即可。
For me the only solution is to modify the returned object at the server level.
If you can't,just map that returns the server.
您可以注册自己的解析器。我必须这样做才能修改和清理遗留系统中的一些 JSON。
然后创建你自己的:
You can register your own parser. I had to do this to modify and clean-up some JSON from a legacy system.
And then create your own:
编辑 JSOn 的响应非常困难。更好的方法是只在服务器端进行更改,如 @beber 所说
It's very difficult to editing the response of JSOn. the better way is only to do changes in server side as said by @beber