iOS JSONKit的一些常用方法记录
来源:互联网 发布:淘宝网韩都衣舍旗舰店 编辑:程序博客网 时间:2024/04/29 10:39
在项目中与服务器交互的时候,经常要遇到解析json的情况,如果有同学想要解析JSON,那么JSONKit可以是一个不错的选择
首先可以去gitHub上下载JSONKit 地址:JSONKit
放入工程以后,把JSONKit设置为不支持arc的模式
然后在点m修改2个地方
//array->isa = _JKArrayClass; object_setClass(array, _JKArrayClass);// dictionary->isa = _JKDictionaryClass; object_setClass(dictionary, _JKDictionaryClass);
原因是isa已经被废弃了,所以请调用object_setClass()和object_getClass()函数来搞定这里特别设置了2个json串,供大家参考学习
arr.conf
[{"preview_sub": "adornment_fly_1_100.png", "width": "300", "path": "adornment_fly_1.png", "name": "adornment_fly_1.png", "height": "300"}, {"preview_sub": "adornment_fly_2_100.png", "width": "300", "path": "adornment_fly_2.png", "name": "adornment_fly_2.png", "height": "300"}]
dict.conf
{"status": "ok", "after": "14851", "before": "11011"}
NSString *res = nil; //数组转json串 NSArray *arr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil]; res = [arr JSONString]; NSLog(@"res= %@", [NSString stringWithString: res]); //字典类型(对象)转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]); //json串转数组 NSString* arrConf = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"arr" ofType:@"conf"] encoding:NSUTF8StringEncoding error:nil]; NSArray *ConfArr = [arrConf objectFromJSONString]; NSLog(@"%@",ConfArr); //json串转字典 NSString* dicConf = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dict" ofType:@"conf"] encoding:NSUTF8StringEncoding error:nil]; NSArray *ConfDic = [dicConf objectFromJSONString]; NSLog(@"%@",ConfDic);
源码:JsonKitDemo
0 0
- iOS JSONKit的一些常用方法记录
- iOS常用的一些方法
- IOS字典的一些常用记录
- 记录一些常用的utils方法
- 动画一些常用的方法记录
- YII CDbCriteria 的一些常用方法记录
- 一些常用的查找方法,记录备用
- 记录一些常用的utils方法3
- 记录一些常用的utils方法2
- 记录一些常用的utils方法4
- 记录一些常用的utils方法5
- 记录一些常用的utils方法6
- IOS之JSONKit的使用
- ios的常用的一些方法
- iOS开发中一些常用的方法
- iOS 开发 一些常用的公共方法
- iOS UITableView的一些常用属性,方法
- iOS中常用的一些小方法
- VHDL 音乐盒设计
- ORACLE 动态执行SQL语句
- 黑马程序员_面向对象的学习总结
- 每天工作4小时的程序员(关于工作效率的思考)
- 双选会攻略
- iOS JSONKit的一些常用方法记录
- ORACLE表空间的碎片整理 20131224
- OpenWRT添加模块(三)内核模块
- MyBatis中Like语句使用方式
- BUG,错误的反思:20131224
- Uboot官方下载地址
- Leetcode: Sum Root to Leaf Numbers
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
- MacOS 下如何制作 .icns 格式的图标文件