1 使用NSJSONSerialization解析
这个网址获取天气信息需要申请key值,试用期小于1天。
-(void)JSONSerializationParse{ NSURL *url = [NSURL URLWithString:@"http://api.36wu.com/Weather/GetWeather"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; request.HTTPMethod = @"POST"; //district表示区域id,authkey申请的试用值,只允许试用一天。所以过了2016/9/9就获取不到数据了,key自己去申请试用。 request.HTTPBody = [@"district=101010100&format=json&authkey=b0aa5ef944514793812734e0b36e5740" dataUsingEncoding:NSUTF8StringEncoding]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * response, NSData * data, NSError * connectionError) { NSLog(@"conncetionError:%@",connectionError); NSLog(@"string:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"dictionary:%@",dictionary); NSDictionary *dataDictionary = dictionary[@"data"]; NSLog(@"weather=%@,refreshTime=%@",dataDictionary[@"weather"],dataDictionary[@"refreshTime"]); }]; }打印如下:
2016-09-09 15:59:01.500 JSON解析[942:983924] conncetionError:(null) 2016-09-09 15:59:01.502 JSON解析[942:983924] string:{"data":{"areaid":101010100,"prov":"北京","city":"北京","district":"北京","dateTime":"2016年9月9日","temp":"31℃","minTemp":"18℃","maxTemp":"31℃","weather":"晴","windDirection":"西风","windForce":"2级","humidity":"27%","img_1":"1","img_2":"4","refreshTime":"15:54"},"message":"OK","status":200} 2016-09-09 15:59:01.503 JSON解析[942:983924] dictionary:{ data = { areaid = 101010100; city = "\U5317\U4eac"; dateTime = "2016\U5e749\U67089\U65e5"; district = "\U5317\U4eac"; humidity = "27%"; "img_1" = 1; "img_2" = 4; maxTemp = "31\U2103"; minTemp = "18\U2103"; prov = "\U5317\U4eac"; refreshTime = "15:54"; temp = "31\U2103"; weather = "\U6674"; windDirection = "\U897f\U98ce"; windForce = "2\U7ea7"; }; message = OK; status = 200; } 2016-09-09 15:59:01.508 JSON解析[942:983924] weather=晴,refreshTime=15:542 使用JSONKit解析
需要向工程中导入JSONKit.h和JSONKit.m文件。使用处需要引头文件
#import "JSONKit.h"
由于我导入的这个版本不支持ARC,所以需要对JSONKit.m文件设置禁止ARC -(void)JSONKitParse{ NSURL *url = [NSURL URLWithString:@"http://api.36wu.com/Train/GetTicketInquiry"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; request.HTTPMethod = @"POST"; //查询火车票接口 request.HTTPBody = [@"startStation=北京&arriveStation=上海&date=2016-10-01&authkey=b0aa5ef944514793812734e0b36e5740" dataUsingEncoding:NSUTF8StringEncoding]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * response, NSData * data, NSError * connectionError) { NSLog(@"conncetionError:%@",connectionError); NSLog(@"string:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //使用JSONkit方法 NSDictionary *dictionary = [data objectFromJSONData]; NSLog(@"dictionary:%@",dictionary); NSArray *dataAry = dictionary[@"data"]; NSLog(@"dataAry count=%lu",(unsigned long)dataAry.count); }]; }由于数据太多就不全贴出来了。
//只是打印的string中的一部分 {"train_no":"240000G1010C","train_code":"G101","start_station":"北京南","end_station":"上海虹桥","from_station":"北京南","to_station":"上海虹桥","start_time":"06:44","arrive_time":"12:38","day_difference":"0","lishi":"05:54","from_station_no":"01","to_station_no":"11","gg_num":"--","gr_num":"--","qt_num":"--","rw_num":"--","rz_num":"--","tz_num":"--","wz_num":"--","yb_num":"--","yw_num":"--","yz_num":"--","ze_num":"522","zy_num":"47","swz_num":"21"} //这是dictionary中的一部分 { "arrive_time" = "12:38"; "day_difference" = 0; "end_station" = "\U4e0a\U6d77\U8679\U6865"; "from_station" = "\U5317\U4eac\U5357"; "from_station_no" = 01; "gg_num" = "--"; "gr_num" = "--"; lishi = "05:54"; "qt_num" = "--"; "rw_num" = "--"; "rz_num" = "--"; "start_station" = "\U5317\U4eac\U5357"; "start_time" = "06:44"; "swz_num" = 21; "to_station" = "\U4e0a\U6d77\U8679\U6865"; "to_station_no" = 11; "train_code" = G101; "train_no" = 240000G1010C; "tz_num" = "--"; "wz_num" = "--"; "yb_num" = "--"; "yw_num" = "--"; "yz_num" = "--"; "ze_num" = 522; "zy_num" = 47; }3使用SBJson解析方法
需要导入SBJson.h文件
#import "SBJson.h"
-(void)SBJsonParse{ NSURL *url = [NSURL URLWithString:@"http://api.36wu.com/Translate/GetTranslate"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; request.HTTPMethod = @"POST"; //翻译,不过服务下限了,翻译不出来。不过可以看一下SBJson解析。 request.HTTPBody = [@"q=今天天气很好&output=zh_en&format=json&authkey=b0aa5ef944514793812734e0b36e5740" dataUsingEncoding:NSUTF8StringEncoding]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * response, NSData * data, NSError * connectionError) { NSLog(@"conncetionError:%@",connectionError); NSLog(@"string:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //使用SBJson方法解析 SBJsonParser *sbJsonParse = [[SBJsonParser alloc] init]; NSDictionary *dictionary = [sbJsonParse objectWithData:data]; NSLog(@"dictionary:%@",dictionary); }]; }过期了,没能正确翻译。 2016-09-09 17:12:52.218 JSON解析[985:995210] conncetionError:(null) 2016-09-09 17:12:52.220 JSON解析[985:995210] string:{"status":200,"message":"OK","data":[{"dst":"旧服务已下线,请迁移至 ","src":"The previous service has been closed, please visit to apply for new service."}]} 2016-09-09 17:12:52.223 JSON解析[985:995210] dictionary:{ data = ( { dst = "\U65e7\U670d\U52a1\U5df2\U4e0b\U7ebf\Uff0c\U8bf7\U8fc1\U79fb\U81f3 "; src = "The previous service has been closed, please visit to apply for new service."; } ); message = OK; status = 200; }
4 使用TouchJson解析
解析需要引入头文件
#import "CJSONDeserializer.h"
转换某对象到JSON数据——即生成,序列化操作需要引入文件: