Objective - c JsonKit 进行json转对象 对象转json数据 & Jastor json数据转为对象
2014-05-07 18:07
316 查看
在ios平台做通用的json数据解析,直接将json格式字符串转化成 对应的Object类(比如:jsonUser 转 User对象)。
思路: 1、 获取服务器端的json数据,然后解析成NSDictionary对象(我们一般是使用第三方的json解析工具JSONKit,SBJson等)。
2、 使用第三方工具Jastor将NSDictionary 转成 响应Object对象。(Jastor下载和用法参考https://github.com/elado/jastor,在该地址下载下Jastor包后,直接将Jastor文件夹拉到ios项目中,只有4个文件Jastor.h、Jastor.m、JastorRuntimeHelper.h、JastorRuntimeHelper.m)
开始编码:
You have the following JSON:
json数据
定义对象
更多节点。。。
NSArray...
JSON和Object-C中数据类型的映射关系如下表所示
使用JSONKit 进行加密解密操作
思路: 1、 获取服务器端的json数据,然后解析成NSDictionary对象(我们一般是使用第三方的json解析工具JSONKit,SBJson等)。
2、 使用第三方工具Jastor将NSDictionary 转成 响应Object对象。(Jastor下载和用法参考https://github.com/elado/jastor,在该地址下载下Jastor包后,直接将Jastor文件夹拉到ios项目中,只有4个文件Jastor.h、Jastor.m、JastorRuntimeHelper.h、JastorRuntimeHelper.m)
开始编码:
You have the following JSON:
json数据
{ "name": "Foo", "amount": 13 }
定义对象
@interface Product @property (nonatomic, copy) NSString *name; @property (nonatomic, retain) NSNumber *amount; @end @implementation Product @synthesize name, amount; - (void)dealloc { self.name = nil; self.amount = nil; [super dealloc]; } @end
// Product.m @implementation Product @synthesize name, amount; - (void)dealloc { self.name = nil; self.amount = nil; [super dealloc]; } @end // Some other code NSDictionary *dictionary = /* parse the JSON response to a dictionary */; Product *product = [[Product alloc] initWithDictionary:dictionary]; // Log product.name // => Foo product.amount // => 13
更多节点。。。
// JSON { "name": "Foo", "category": { "name": "Bar Category" } }
// ProductCategory.h @interface ProductCategory : Jastor @property (nonatomic, copy) NSString *name; @end // ProductCategory.m @implementation ProductCategory @synthesize name; - (void)dealloc { self.name = nil; [super dealloc]; } @end // Product.h @interface Product : Jastor @property (nonatomic, copy) NSString *name; @property (nonatomic, retain) ProductCategory *category; @end // Product.m @implementation Product @synthesize name, category; - (void)dealloc { self.name = nil; self.category = nil; [super dealloc]; } @end // Code NSDictionary *dictionary = /* parse the JSON response to a dictionary */; Product *product = [[Product alloc] initWithDictionary:dictionary]; // Log product.name // => Foo product.category // => <ProductCategory> product.category.name // => Bar Category
NSArray...
// JSON { "name": "Foo", "categories": [ { "name": "Bar Category 1" }, { "name": "Bar Category 2" }, { "name": "Bar Category 3" } ] }
// ProductCategory.h @interface ProductCategory : Jastor @property (nonatomic, copy) NSString *name; @end // ProductCategory.m @implementation ProductCategory @synthesize name; - (void)dealloc { self.name = nil; [super dealloc]; } @end // Product.h @interface Product : Jastor @property (nonatomic, copy) NSString *name; @property (nonatomic, retain) NSArray *categories; @end // Product.m @implementation Product @synthesize name, categories; + (Class)categories_class { return [ProductCategory class]; } - (void)dealloc { self.name = nil; self.categories = nil; [super dealloc]; } @end // Code NSDictionary *dictionary = /* parse the JSON response to a dictionary */; Product *product = [[Product alloc] initWithDictionary:dictionary]; // Log product.name // => Foo product.categories // => <NSArray> [product.categories count] // => 3 [product.categories objectAtIndex:1] // => <ProductCategory> [[product.categories objectAtIndex:1] name] // => Bar Category 2
+ (Class)categories_class { return [ProductCategory class]; }
JSON和Object-C中数据类型的映射关系如下表所示
JSON | Objective-C |
---|---|
null | NSNull |
trueand false | NSNumber |
Number | NSNumber |
String | NSString |
Array | NSArray |
Object | NSDictionary |
#import "JSONKit.h" @interface BYViewController () @end @implementation BYViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *res = nil; /* * json格式编码 对象 转json 对象加密成json数据 */ //字符串 字符串转json NSString *str = @"this is a nsstring"; res = [str JSONString]; NSLog(@"res= %@", [NSString stringWithString: res]); //res= "this is a nsstring" //数组 数组 转 json NSArray *arr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil]; res = [arr JSONString]; NSLog(@"res= %@", [NSString stringWithString: res]); //res= ["One","Two","Three"] //字典类型(对象) 对象 转 json NSArray *arr1 = [NSArray arrayWithObjects:@"dog",@"cat",nil]; NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithInt:30],nil]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil]; res = [dic JSONString]; NSLog(@"res= %@", [NSString stringWithString: res]); //res= {"pets":["dog","cat"],"other":[true,30]} /* * json格式解码 json 转对象 json解密为对象 */ JSONDecoder *jd=[[JSONDecoder alloc] init]; //针对NSData数据 json 转nsData NSData *data = [dic JSONData]; NSDictionary *ret = [jd objectWithData: data]; NSLog(@"res= %@", [ret objectForKey:@"pets"]); //res= ( // dog, // cat //) NSLog(@"res= %@", [[ret objectForKey:@"other"] objectAtIndex:0]); //res= 1 //针对NSString字符串数据 json 转nsstring NSString *nstr = [dic JSONString]; NSDictionary *ret2 = [jd objectWithUTF8String:(const unsigned char *)[nstr UTF8String] length:(unsigned int)[nstr length]]; NSLog(@"res= %d", [[ret2 objectForKey:@"pets"] indexOfObject:@"cat"]); //res= 1 NSLog(@"res= %@", [[ret2 objectForKey:@"other"] objectAtIndex:1]); //res= 30 } @end
相关文章推荐
- 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name&value>格式和JSON格式。
- java对象、List转为json格式的数据
- Unity3D学习(二):使用JSON进行对象数据的存储读取
- <Android>在Activity被非正常杀死并重建时,使用savedInstanceState进行数据恢复的简单实例
- js中字符串数据转为json对象的方法
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 serlet
- iOS中使用KVC实现JSON数据与Objective-C实体对象之间的转换
- js中字符串数据转为json对象的方法
- <项目一>java数据转为JSONObject,再转为JSONArray
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 serlet配置页面
- iOS中使用KVC实现JSON数据与Objective-C实体对象之间的转换
- 在json封装成对象返回前台数据中,null和""区别
- 聚合对象根据模板数据转为JSON数据方案
- C#处理Json数据/Json数据转为对象
- eval解析JSON中的注意点 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数。 2. 使用Function对象来进行返回解析。 使用eval
- 对象转换为JSON数据格式&使用JQuery获取数据
- java对象、List转为json格式的数据(最后是json字符串格式,不是json对象)
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- Json数据解析----------Gson用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库
- php 解决 json_encode 数据进入数据库 把"\" 去掉的问题