iOS开发使用NSJSONSerialization、JSONKit、SBJson、TouchJson四种方式解析Json数据
概括:JSON数据解析可以使用苹果自带的NSJSONSerialization方式,也可以使用第三方框架,比如JSONKit、SBJson、TouchJSON等,框架的使用方式比较简单,但性能比ios原生方式差很多,建议使用原生方式。
1 NSJSONSerialization,IOS原生
/** * 解析JSON数据 */ -(void) analysisJSON{ // 获取URL NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:8070/MJServer/video"]; // 获取session的实例 NSURLSession *session = [NSURLSession sharedSession]; // 创建一个数据访问任务 NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // 如果访问错误 if (error || data == nil) { NSLog(@"网络繁忙,请稍后再试"); return; } // 解析JSON数据,预先知道json数据的结构 NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSArray *videos = result[@"videos"]; for (NSDictionary *dict in videos) { NSLog(@"videoID -- %@", dict[@"id"]); NSLog(@"videoName -- %@", dict[@"name"]); NSLog(@"videoURL -- %@", dict[@"url"]); printf("\n----------------华丽丽的分割线-----------------\n"); } }]; [task resume]; }
2 JSONKit,需要导入#import "JSONKit.h",并且要添加libz.dylib动态库
/** * JSONKit */ - (IBAction)jsonkit { NSLog(@"jsonkit"); NSDictionary *weatherInfo = [[self weatherData] objectFromJSONData]; self.jsonData.text = [self weatherFormat:weatherInfo]; }
3 SBJson,需要导入#import "SBJson/SBJson.h"
/** * SBJson */ - (IBAction)sbjson { NSLog(@"sbjson"); NSURL *url = [[NSURL alloc] initWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"]; NSString *responseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; // 创建SBJson分析对象 SBJsonParser *parser = [[SBJsonParser alloc] init]; // 从json数据中获取字典 NSDictionary *weatherInfo = [parser objectWithString:responseString]; self.jsonData.text = [self weatherFormat:weatherInfo]; }
4 TouchJson,需要导入#import "TouchJson/JSON/CJSONDeserializer.h"
/** * TouchJson */ - (IBAction)touchjson { NSLog(@"touchjson"); NSDictionary *weatherInfo = [[CJSONDeserializer deserializer]deserialize:[self weatherData] error:nil]; self.jsonData.text = [self weatherFormat:weatherInfo]; }
2\3\4以天气信息为例,其数据获取方法及格式化方法如下
/** * 获取天气信息 * { "weatherinfo": { "city": "北京", "cityid": "101010100", "temp1": "12℃", "temp2": "15℃", "weather": "小雨", "img1": "n7.gif", "img2": "d7.gif", "ptime": "18:00" } } * @return json数据 */ -(NSData *)weatherData{ NSURL *url = [[NSURL alloc] initWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *weather = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"weather -- %@", weather[@"weatherinfo"]); return data; } // 天气格式化 -(NSString *)weatherFormat:(NSDictionary *)weatherInfo{ NSDictionary *weather = weatherInfo[@"weatherinfo"]; NSString *city =weather[@"city"]; NSString *status =weather[@"weather"]; NSString *temp1 =weather[@"temp1"]; NSString *temp2 =weather[@"temp2"]; NSString *ptime =weather[@"ptime"]; return [NSString stringWithFormat:@"天气预报(%@发布)\n%@,%@,气温%@-%@", ptime, city, status, temp1, temp2]; }