JSONKit在项目中使用设置
2016-06-01 00:00
337 查看
在项目中经常会遇到解析json的情况,如果想要解析JSON,那么JSONKit可以是一个不错的选择。
git中JSONKit 的地址为:https://github.com/johnezang/JSONKit
由于项目已经很久没有更新,仍然使用了ARC,因此在使用时需要做几处修改:
1.把JSONKit设置为不支持arc的模式,在Build Phases ->Compile Sources 选择文件双击,在对话框中添加“-fno-objc-arc”参数(不含引号)。
2.此时编译仍然会出现下面的报警:
报错信息:error: assignment to Objective-C‘s isa is deprecated in favor of object_setClass()
解决办法:
(1)修改JSONKit.m文件第680行,修改为object_setClass(array, _JKArrayClass);
(2)修改JSONKit.m文件第931行,修改为object_setClass(dictionary, _JKDictionaryClass);
3.代码实现
用法:
1.dictionary------>json
NSString *jsonstring = [dictionary JSONString];
2.json------------>dictionary
NSDictionary *dictionary = [jsonstring objectFromJSONString];
//string to dictionary
NSString *resultStr = @"{\"name\": \"admin\",\"list\": [\"one\",\"two\",\"three\"]}";
NSData* jsonData = [resultStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", [jsonData class]);
NSDictionary *resultDict = [jsonData objectFromJSONData];
NSLog(@"name is :%@",[resultDict objectForKey:@"name"]);
NSArray *list = [resultDict objectForKey:@"list"];
for (NSString *str in list) {
NSLog(@"list res:%@",str);
}
//dicttionary to string
NSString *jsonStr = [resultDict JSONString];
NSLog(@"temp is :%@",jsonStr);
git中JSONKit 的地址为:https://github.com/johnezang/JSONKit
由于项目已经很久没有更新,仍然使用了ARC,因此在使用时需要做几处修改:
1.把JSONKit设置为不支持arc的模式,在Build Phases ->Compile Sources 选择文件双击,在对话框中添加“-fno-objc-arc”参数(不含引号)。
2.此时编译仍然会出现下面的报警:
报错信息:error: assignment to Objective-C‘s isa is deprecated in favor of object_setClass()
解决办法:
(1)修改JSONKit.m文件第680行,修改为object_setClass(array, _JKArrayClass);
(2)修改JSONKit.m文件第931行,修改为object_setClass(dictionary, _JKDictionaryClass);
3.代码实现
用法:
1.dictionary------>json
NSString *jsonstring = [dictionary JSONString];
2.json------------>dictionary
NSDictionary *dictionary = [jsonstring objectFromJSONString];
//string to dictionary
NSString *resultStr = @"{\"name\": \"admin\",\"list\": [\"one\",\"two\",\"three\"]}";
NSData* jsonData = [resultStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", [jsonData class]);
NSDictionary *resultDict = [jsonData objectFromJSONData];
NSLog(@"name is :%@",[resultDict objectForKey:@"name"]);
NSArray *list = [resultDict objectForKey:@"list"];
for (NSString *str in list) {
NSLog(@"list res:%@",str);
}
//dicttionary to string
NSString *jsonStr = [resultDict JSONString];
NSLog(@"temp is :%@",jsonStr);
相关文章推荐
- NSJSONSerialization介绍
- JavaScript事件
- 11. jsp与servlet之间页面跳转及参数传递实例
- javascript(八) 函数
- javascript 获取浏览器信息
- javascript(九) 事件捕获/冒泡
- 总结一下Ecmascript和Javascript不一样的地方(1)
- JSP页面获取 list 的长度
- JS前台数据校验(常用)留底备份
- JS函数(内置函数)
- tab.js分享及浏览器兼容性问题汇总
- tab.js分享及浏览器兼容性问题汇总
- Jstorm 集群搭建过程/Jstorm集群一键安装部署
- HTML/CSS/JavaScript实现的2048小游戏
- js !!条件判断或运算的作用
- Createjs学习一
- 谈谈javascript语法里一些难点问题(二)
- 谈谈javascript语法里一些难点问题(一)
- js的性质和一些对象及属性
- 学习RxJS: 导入