AFNetWorking是比较常用的网络解析的库,今天研究一下返回的Json格式以及其中内容的解析。下载地址:https://github.com/AFNetworking/AFNetworking/ 里面也有Example,不过确实看不懂写的什么。所以自己尝试用自己的方式来解释一下这个东东。 首先新建一个项目,将AFNetworking,添加至项目中。在pch添加预编译头文件。 StoryBoard中一个页面+一个按钮就可以。我这个例子,主要是看返回的Json数据都有哪些东东,以及提取数据。 TestVC.h代码: #import <UIKit/UIKit.h> @interface TestVC : UIViewController - (IBAction)jsonTap:(id)sender; @end .m文件代码: #import "TestVC.h" static NSString *const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/"; //声明一个静态变量,这个静态变量就是下json的地址 @interface TestVC () @property(strong,nonatomic)NSDictionary *wDic; //存储天气的json @property(strong,nonatomic)NSArray *wKey; //存储天气的key @end - (IBAction)jsonTap:(id)sender { NSString *weatherURL= [NSString stringWithFormat:@"%@weather.php?format=json",BaseURLString]; //得到json的URL NSURL *url=[NSURL URLWithString:weatherURL]; //将字符串转为URL NSURLRequest *request=[NSURLRequest requestWithURL:url]; //新建一个针对URL的请求 AFJSONRequestOperation *operation=[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request,NSHTTPURLResponse *response,id json){ self.wDic=(NSDictionary *)json; //将json转换为字典并存储 } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"未下载" message: [NSString stringWithFormat:@"%@",error]delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [av show]; }]; [operation start]; //-------打印Json数据的代码----------- self.wKey=[self.wDic allKeys]; for (NSString *key in self.wKey) { NSLog(@"key为:%@,值为:%@",key,[self.wDic objectForKey:key]); } //-------打印Json数据的代码----------- } 打印出的Json数据为: 2013-06-07 09:58:04.460 AFK_Study[779:c07] key为:data,值为:{ "current_condition" = ( { cloudcover = 16; humidity = 59; "observation_time" = "09:09 PM"; precipMM = "0.1"; pressure = 1010; "temp_C" = 10; "temp_F" = 49; visibility = 10; weatherCode = 113; weatherDesc = ( { value = Clear; } ); weatherIconUrl = ( { value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png"; } ); winddir16Point = NW; winddirDegree = 316; windspeedKmph = 47; windspeedMiles = 29; } ); request = ( { query = "Lat 32.35 and Lon 141.43"; type = LatLon; } ); weather = ( { date = "2013-01-15"; precipMM = "1.8"; tempMaxC = 12; tempMaxF = 53; tempMinC = 10; tempMinF = 50; weatherCode = 119; weatherDesc = ( { value = Cloudy; } ); weatherIconUrl = ( { value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"; } ); winddir16Point = NNW; winddirDegree = 348; winddirection = NNW; windspeedKmph = 66; windspeedMiles = 41; }, { date = "2013-01-16"; precipMM = "0.6"; tempMaxC = 13; tempMaxF = 56; tempMinC = 11; tempMinF = 51; weatherCode = 113; weatherDesc = ( { value = Sunny; } ); weatherIconUrl = ( { value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"; } ); winddir16Point = WNW; winddirDegree = 284; winddirection = WNW; windspeedKmph = 33; windspeedMiles = 21; }, { date = "2013-01-17"; precipMM = "0.5"; tempMaxC = 14; tempMaxF = 56; tempMinC = 7; tempMinF = 44; weatherCode = 119; weatherDesc = ( { value = Cloudy; } ); weatherIconUrl = ( { value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"; } ); winddir16Point = WNW; winddirDegree = 293; winddirection = WNW; windspeedKmph = 41; windspeedMiles = 25; }, { date = "2013-01-18"; precipMM = "1.9"; tempMaxC = 11; tempMaxF = 51; tempMinC = 7; tempMinF = 44; weatherCode = 353; weatherDesc = ( { value = "Light rain shower"; } ); weatherIconUrl = ( { value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png"; } ); winddir16Point = NW; winddirDegree = 312; winddirection = NW; windspeedKmph = 66; windspeedMiles = 41; }, { date = "2013-01-19"; precipMM = "1.1"; tempMaxC = 7; tempMaxF = 45; tempMinC = 6; tempMinF = 43; weatherCode = 176; weatherDesc = ( { value = "Patchy rain nearby"; } ); weatherIconUrl = ( { value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png"; } ); winddir16Point = NW; winddirDegree = 324; winddirection = NW; windspeedKmph = 52; windspeedMiles = 32; } ); } OK,json的数据已经出来了,接下来就是分析和截取了。 |
AFNetWorking-1-获得数据
最新推荐文章于 2021-02-12 14:05:28 发布