在项目中常常会遇到解析json的状况,若是想要解析JSON,那么JSONKit能够是一个不错的选择。git
git中JSONKit 的地址为:https://github.com/johnezang/JSONKitgithub
因为项目已经好久没有更新,仍然使用了ARC,所以在使用时须要作几处修改:json
1.把JSONKit设置为不支持arc的模式,在Build Phases ->Compile Sources 选择文件双击,在对话框中添加“-fno-objc-arc”参数(不含引号)。ui
2.此时编译仍然会出现下面的报警:three
报错信息:error: assignment to Objective-C‘s isa is deprecated in favor of object_setClass()string
解决办法:it
(1)修改JSONKit.m文件第680行,修改成object_setClass(array, _JKArrayClass);io
(2)修改JSONKit.m文件第931行,修改成object_setClass(dictionary, _JKDictionaryClass);编译
3.代码实现class
用法:
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);