我是来自 Java 世界的 Objective-C 新手,所以我正在尝试找出如何做一些在 Java 中(相对)简单易行的事情。
在 iPhone 应用程序中,我正在访问返回 JSON 的休息网络服务。我正在使用 AFNetworking 框架,它支持使用添加到 iOS 5 的 NSJSONSerialization 类。我想知道是否有一种“自动”方式将返回的 JSON 对象转换为我的“强”类型类之一(例如,我的 UserAccount 类)。
换句话说,我正在尝试做类似的事情:
id json = ... call webservice that returns json, which AFNetworking parses ...
UserAccount myUserAccount = [UserAccount alloc] init];
//then here I'd like something that iterates through all the properties on the
//json object and sets them on the myUserAccount object using key value coding
有没有一种简单的方法可以做到这一点?
最佳答案
不,据我所知不是。最常见的方法是向名为 initWithDictionary
的类添加一个构造函数,它接受 JSON 字典并从中获取相关信息。
关于objective-c - 使用键值编码将 JSON 转换为 Objective-C 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301681/