通过RestKit将本地NSString的JSON反序列化为对象(无需网络下载)
是否可以通过 RestKit 将 JSON 的 NSString
反序列化为对象?我在此处检查了 API 列表,但找不到可用于此目的的内容。我能找到的最接近的是解析输入后返回 NSDictionary 的各种解析器类。我假设 RestKit 在下载响应后使用这些解析器,所以我的想法是该功能在 RestKit 中的某个地方可用,但没有公开公开。
如果我没有遗漏任何东西并且没有公开此功能,那么替代方案是什么?两个明显的看起来不太有希望:获取结果 NSDictionary
并尝试反序列化自己(有效地重新实现 RestKit)或尝试深入研究 RestKit 源代码并查看是否可以以某种方式暴露(看起来乏味且错误)易于)。
预先感谢您的任何帮助。
PS:这个想法是,反序列化对象上的字符串属性实际上是另一组对象的 JSON 表示(某种意义上是嵌入式 JSON),并且在运行时根据需要进行反序列化。
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.
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).
Thanks in advance for any help.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
相当“简单”:
Pretty "simple":
自 RestKit 0.20.0-pre2 起
As of RestKit 0.20.0-pre2
这适用于 Restkit 0.21.0:
This works for Restkit 0.21.0:
这适用于 Restkit 0.20,使用核心数据实体。它基于@innerself给出的解决方案
This works for Restkit 0.20, using Core Data Entities. It is based in the solution given by @innerself
您可以在
RKManagedObjectResponseMapperOperation
类中了解 RestKit 如何在内部执行此操作。此操作分为三个阶段。
首先是将 JSON 字符串解析为 NSDictionarys、NSArrays 等。这是最简单的部分。
接下来,您需要运行映射操作来将此数据转换为 NSManagedObjects。这有点涉及更多。
请记住用您自己的映射替换此字典。键
[NSNull null]
从根映射此对象。您现在需要运行已放入我们创建的操作队列中的任务。正是在这个阶段,建立了与现有 NSManagedObject 的连接。
You can see how RestKit does this internally in the
RKManagedObjectResponseMapperOperation
class.There are three stages of this operation.
The first is to parse the JSON string into NSDictionarys, NSArrays, etc. This is the easiest part.
Next you need to run a mapping operation to convert this data into your NSManagedObjects. This is a bit more involved.
Remember to replace this dictionary with your own mappings. The key
[NSNull null]
maps this object from the root.You now need to run the tasks that have been put into the operationQueue we created. It is at this stage that connections to existing NSManagedObjects are made.
一个更面向 iOS 5+ 的答案:
A more iOS 5+ oriented answer:
对于 Restkit 0.22,您可以使用此代码。这将返回一个 RKMappingResult,您可以在其中使用属性 .array 枚举映射后的对象。
For Restkit 0.22, You can use this code. This returns an RKMappingResult wherein you can enumerate the objects after mapping using the property .array.
这不是您要找的吗? http://restkit.org/api/0.10.0/Classes/RKJSONParserJSONKit.html
Is this not what you're looking for? http://restkit.org/api/0.10.0/Classes/RKJSONParserJSONKit.html
从没有任何答案的观点来看,RestKit 中似乎还不存在这个功能。我没有花更多时间尝试弄清楚如何进行映射,而是使用 JsonKit 解析器的输出编写了自己的映射器,并删除了对 RestKit 的依赖(使用内置类进行网络活动)。现在我的映射器不是通用的(它对对象的布局方式及其在 json 中的名称有一些依赖),但它适用于项目的目的。我可能会稍后回来并将其变成一个更通用的对象映射库。
编辑:这是选定的答案,因为截至此答案的日期(2012 年 1 月 21 日)没有其他答案。从那时起,我就停止了 iOS 的工作,再也没有访问过这个问题。现在我选择 Ludovic 的答案是因为另一位用户的评论和对该答案的赞同。
Judging by the views without any answers, it seems this facility does not exist in RestKit yet. Instead of spending more time trying to figure out how to do the mapping, I wrote my own mapper using the output of JsonKit parser and removed the dependency on RestKit (used the builtin classes for network activity). Right now my mapper is not generic (it has a few dependencies on how the objects are laid out and their names in json) but it works for the purposes of the project. I might come back later and turn it into a more generic object mapping library later on.
EDIT: This was selected answer because there was no other answer as of this answer's date (Jan 21, 2012). Since then, I stopped working on iOS and never visited this question again. Now I am selecting Ludovic's answer because of another user's comment and the upvotes for that answer.