通过RestKit将JSON的本地NSString反序列化为对象(无网络下载) [英] Deserializing local NSString of JSON into objects via RestKit (no network download)

查看:109
本文介绍了通过RestKit将JSON的本地NSString反序列化为对象(无网络下载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过RestKit将JSON的 NSString 反序列化为对象?我检查了此处的API列表,但找不到可用于此目的的内容。我能找到的最接近的是解析输入后返回 NSDictionary 的各种解析器类。我假设RestKit在下载响应后使用这些解析器,所以我的想法是在RestKit中的某个地方可以使用这个功能但是没有公开公开。

Is it possible to deserialize an NSString of JSON into objects via RestKit? I checked the API list here and could not find something that would serve for this purpose. The closest I could find are the various parser classes that return NSDictionary after parsing the input. I assume RestKit uses these parsers after downloading the response so my thinking is that the functionality is available somewhere in RestKit but not exposed publicly.

如果我没有遗漏任何东西,这个功能没有暴露,会有什么替代方案?两个明显的看起来不太有希望:获得结果 NSDictionary 并尝试反序列化自己(有效地重新实现RestKit)或尝试深入RestKit源并查看是否可以某种方式暴露(看起来很乏味且容易出错)。

If I am not missing anything and this functionality is not exposed, what would be the alternatives? Two obvious ones do not look very promising: Get the resulting NSDictionary and try to deserialize myself (effectively reimplementing RestKit) or try to dive into RestKit source and see if this can be somehow exposed (looks tedious and error prone).

提前感谢您的帮助。

PS:这个想法是反序列化对象上的字符串属性实际上是另一组对象的JSON表示(在某种意义上是嵌入式JSON),并且在运行时根据需要进行反序列化。

PS: The idea is that a string property on a deserialized object is actually the JSON representation of another set of objects (embedded JSON in a sense) and it is deserialized on demand during runtime.

推荐答案

漂亮简单:

NSString *stringJSON;
...

RKJSONParserJSONKit *parser;
NSError *error= nil;
parser= [[[RKJSONParserJSONKit alloc] init] autorelease]; 
MyManagedObject *target;
target= [MyManagedObject object];

NSDictionary *objectAsDictionary;
RKObjectMapper* mapper;
objectAsDictionary= [parser objectFromString:stringJSON error:&error];
mapper = [RKObjectMapper mapperWithObject:objectAsDictionary 
                          mappingProvider:[RKObjectManager sharedManager].mappingProvider];
mapper.targetObject = target;
RKObjectMappingResult* result = [mapper performMapping];
NSLog(@"%@", [result asObject]);

这篇关于通过RestKit将JSON的本地NSString反序列化为对象(无网络下载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆