使用RestKit和Objective-C映射JSON响应对对象的响应
-
25-10-2019 - |
题
我对Objective-C是相对较新的,并且正在尝试使用RESTKIT接收Web服务的JSON响应。我已经成功地将数据收回给我的应用程序,看起来像是查看响应:
{id:"1","Translation":"Test"}
我想将此翻译映射到我的应用程序中的“翻译”对象,但尝试了几种不同的方法,但不确定如何实现这一目标。
所以我的问题是:
- 我如何将此响应映射到我的翻译对象
- 我是否正确执行此操作,创建一种完成此通话的方法,超过了我的视图控制器?
我的翻译对象
@implementation Translation
@synthesize identifier = _identifier;
@synthesize translation = _translation;
- (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"id", @"identifier",
@"translation", @"translation",
nil];
}
@end
我的翻译方法
- (NSString *)performTranslation:(NSString *)translation
{
NSString *data = [[NSString alloc] initWithFormat:@"{\"SourceId\": \"%@\",\"RegionTag\": \"%@\",\"InputString\": \"%@\"}", @"1", @"Glasgow", translation];
NSString *post = data;
RKRequest *MyRequest = [[RKRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"http://my.url.com/Translation/Translate"]];
MyRequest.method = RKRequestMethodPOST;
MyRequest.HTTPBodyString = post;
MyRequest.additionalHTTPHeaders = [[NSDictionary alloc] initWithObjectsAndKeys:@"application/json", @"Content-Type", @"application/json", @"Accept", nil];
[MyRequest send];
RKResponse *Response = [MyRequest sendSynchronously];
return Response.bodyAsString; <--- looking to map this to translation object here
}
解决方案
代码的片段似乎有点过时。我强烈建议阅读最新的 对象映射指南 为了利用RESTKIT发挥最大的潜力 - 尤其是 无KVC的映射.
编辑:
为了用restkit发布一个对象并获得答案,我们定义一个 TranslationRequest
会有我们的请求的课程& Translation
保持我们的回应。
首先,我们设置了我们的 RKObjectManager
和映射(我通常在AppDelegate中这样做):
RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURL:kOurBaseUrl];
[manager setSerializationMIMEType:RKMIMETypeJSON];
//this is a singleton, but we keep the manager variable to avoid using [RKObjectManager sharedManager] all the time
//Here we define a mapping for the request. Note: We define it as a mapping from JSON to entity and use inverseMapping selector later.
RKObjectMapping *translationRequestMapping = [RKObjectMapping mappingForClass:[TranslationRequest class]];
[translationRequestMapping mapKeyPath:@"RegionTag" toAttribute:@"regionTag"];
...
[[manager mappingProvider] setSerializationMapping:[translationRequestMapping inverseMapping] forClass:[TranslationRequest class]];
//now we define the mapping for our response object
RKObjectMapping *translationMapping = [RKObjectMapping mappingForClass:[Translation class]];
[translationMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[translationMapping mapKeyPath:@"Translation" toAttribute:@"translation"];
[[manager mappingProvider] addObjectMapping:mapping];
//finally, we route our TranslationRequest class to a given endpoint
[[manager router] routeClass:[TranslationRequest class] toResourcePath:kMyPostEndpoint];
这应该足够必要的设置。我们可以在代码中的任何地方(例如,在任何控制器中)调用我们的后端:
//we create new TranslationRequest
TranslationRequest *request = [[TranslationRequest alloc] init];
[request setRegionTag:@"Hello"];
....
//then we fetch the desired mapping to map our response with
RKObjectMapping *responseMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:class]
//and just call it. Be sure to let 'self' implement the required RKObjectManagerDelegate protocol
[[RKObjectManager sharedManager] postObject:request mapResponseWith:responseMapping delegate:self];]
尝试这种方法,让我知道您是否需要任何帮助。.我无法对其进行全面测试,因为我没有任何合适的后端可以返回答复,但是从RESTKIT日志来判断这应该有效。
其他提示
您需要将返回的JSON字符串传递到JSON解析器中。我用 SBJSON. 。然后,您可以使用结果字典来填充对象的属性。
RESTKIT似乎具有封装四个不同JSON解析器的本地对象。但是,我建议谨慎,因为他们似乎认为最高级别的对象将永远是词典。
顺便说一句,您的问题中的示例不是有效的JSON。看起来应该这样:
{"id":"1","Translation":"Test"}
不隶属于 StackOverflow