IOS json 转 Object / ios json to Object / ios json convert Object
来源:互联网 发布:最火歌曲2016网络歌曲 编辑:程序博客网 时间:2024/05/21 04:23
直奔主题:在ios平台做通用的json数据解析,直接将json格式字符串转化成 对应的Object类(比如:jsonUser 转 User对象)。
思路: 1. 获取服务器端的json数据,然后解析成NSDictionary对象(我们一般是使用第三方的json解析工具JSONKit,SBJson等)。
2. 使用第三方工具Jastor将NSDictionary 转成 响应Object对象。
ok,现在跟大家列出关键代码:
1.首先我使用的JSONkit将json字符串解析为NSDictionary对象(参考:http://blog.csdn.net/ck89757/article/details/7842846 )
1 //objectFromJSONData直接使用data转字典 2 NSData *data = [@"{\"name\":\"hanqing\",\"age\":\"23\"}" dataUsingEncoding:NSUTF8StringEncoding]; 3 NSDictionary *result = [data objectFromJSONData];//JSONKit中的解析函数 objectFromJSONData
2.创建Object对象,使用Jastor 将NSDictionary 转成 Product对象 (Jastor下载和用法参考https://github.com/elado/jastor,在该地址下载下Jastor包后,直接将Jastor文件夹拉到ios项目中,只有4个文件Jastor.h、Jastor.m、JastorRuntimeHelper.h、JastorRuntimeHelper.m)
1 // Product.h 2 @interface Product : Jastor//一定要继承Jastor类 3 @property (nonatomic, copy) NSString *name; 4 @property (nonatomic, copy) NSNumber *amount; 5 @end 6 7 // Product.m 8 @implementation Product 9 @synthesize name, amount; 10 @end 11 12 // 关键代码 13 Product *product = [[Product alloc] initWithDictionary:result];//Jastor中的转化函数
此博文转自Csdn,但没找到最初出处,待找到更新转载地址。
0 0
- IOS json转对象 /json 转 Object / ios json to Object / ios json convert Object
- IOS json 转 Object / ios json to Object / ios json convert Object
- IOS json 转 Object / ios json to Object / ios json convert Object
- Object convert to Json string
- Convert object to json and json to object in C#
- OBJECT Convert JSON
- ios中json object转换成字符串
- convert Java object to / from JSON
- Convert Xml to json object in android
- c# json to object
- json string to object
- json to object
- json to object
- Java JSON - Convert Java Object To / From JSON (Jackson)
- .net json转换 json to object & object to json
- iOS JSON与Object之间的映射 ObjectMapper For Swift
- How to convert Java object to / from JSON (Gson)
- How to convert Java object to / from JSON (Gson)
- 傅里叶变换资料汇总
- Junit4学习与使用
- mount.nfs: access denied by server while mounting解决由权限引发的问题
- 调试bootsect和setup和head到main
- Spring自定义对象的注入配置
- IOS json 转 Object / ios json to Object / ios json convert Object
- 最大子序列和问题
- 在JAVA项目中如何配置WebService服务器端
- 趣味XOR
- 开发语言小传之一:最早的编程语言——机器语言
- 炮案秸忧舷杏临肮凡永梅揪衬嫡俳
- linux ubuntu12.04 下的 vasp 5.2 的安装方法
- Floyd算法
- 九度OJ 1097 取中值