IOS json 转 Object / ios json to Object / ios json convert Object
2012-08-31 10:26
447 查看
直奔主题:在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 )
[cpp] view
plaincopy
//objectFromJSONData直接使用data转字典
NSData *data = [@"{\"id\":1,\"age\":\"2\"}" dataUsingEncoding:NSUTF8StringEncoding];
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)
[html] view
plaincopy
// Product.h
@interface Product : Jastor//一定要继承Jastor类
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *amount;
@end
// Product.m
@implementation Product
@synthesize name, amount;
@end
// 关键代码
Product *product = [[Product alloc] initWithDictionary:result];//Jastor中的转化函数
思路: 1. 获取服务器端的json数据,然后解析成NSDictionary对象(我们一般是使用第三方的json解析工具JSONKit,SBJson等)。
2. 使用第三方工具Jastor将NSDictionary 转成 响应Object对象。
ok,现在跟大家列出关键代码:
1.首先我使用的JSONkit将json字符串解析为NSDictionary对象(参考:http://blog.csdn.net/ck89757/article/details/7842846 )
[cpp] view
plaincopy
//objectFromJSONData直接使用data转字典
NSData *data = [@"{\"id\":1,\"age\":\"2\"}" dataUsingEncoding:NSUTF8StringEncoding];
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)
[html] view
plaincopy
// Product.h
@interface Product : Jastor//一定要继承Jastor类
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *amount;
@end
// Product.m
@implementation Product
@synthesize name, amount;
@end
// 关键代码
Product *product = [[Product alloc] initWithDictionary:result];//Jastor中的转化函数
相关文章推荐
- IOS json转对象 /json 转 Object / ios json to Object / ios json convert Object
- 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
- iOS JSON NSString Convert to NSDictionary
- Java JSON - Convert Java Object To / From JSON (Jackson)
- convert Java object to / from JSON
- IOS解析json报错-[__NSCFString objectFromJSONString]: unrecognized selector sent to 解决办法
- Convert InputStream to JSONObject
- Object convert to Json string
- How to convert Java object to / from JSON (Gson)
- How to convert Java object to / from JSON (Gson)
- Convert JS object to JSON string
- Convert string to JSONObject by net.sf.json library
- java.lang.String cannot be convert to JSONObject
- Convert object to json and json to object in C#
- convert NameValueCollection/Dictionary<string, object> to JSON string
- ConvertJSONDateToJSDateObject 方法实现json格式时间串转换为 对应的时间格式串
- Json convert to object with gjson.(Json 转换为类对象)
- how to convert a object to json file immediately during debug?