objective-c - 使用键值编码将 JSON 转换为 Objective-C 对象

标签 objective-c ios json

我是来自 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/

相关文章:

ios - 临时存储数据然后模拟-iOS

ios 从 DocumentsDirectory 打开 ibooks 中的 pdf

javascript - 如何传递jsonp格式的javascript代码并执行

javascript - 如何使用 jQuery getJSON() 访问从外部源接收的 JSON 元素?

iphone - iOS5:像通知中心一样跟踪手指并向下拖动 View Controller

ios - 如何在 Objective C 中对 Google Maps maker 的标题添加操作

objective-c - NSString 没有指定 'assign' 、 'retain' 或 'copy' 属性

iOS 开发 - 圆形矩形按钮向下移动子菜单

javascript - 使用javascript将json显示为html

objective-c - 怎么会connectionDidFinishLoading : run if no file is found on server?