陈斌彬的技术博客

Stay foolish,stay hungry

iOS 自带 JSON 解析中国和世界天气预报 JSON 数据(原创)

中国和世界天气预报API网址

http://apistore.baidu.com/apiworks/servicedetail/478.html

img

img

img

img

img

img

代码示例

利用 iOS 自带的 NSJSONSerialization 解析 JSON 数据

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *httpUrl = @"http://apis.baidu.com/heweather/weather/free";
    NSString *httpArg = @"city=jieyang";
    [self request: httpUrl withHttpArg: httpArg];
}

-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg {
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"c33bdd6ad06c082a12a171edc323cc9a" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                               } else {

//                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
//                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//                                   NSLog(@"HttpResponseCode:%ld", responseCode);
//                                   NSLog(@"HttpResponseBody %@",responseString);
//                                   
//                                   

                           //iOS 自带解析类 NSJSONSerialization 从 response 中解析出数据放到字典中
                           NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data  options:NSJSONReadingMutableLeaves error:&error];
                          // NSLog(@"dic is %@",dic);
                           NSArray *list = [dic objectForKey:@"HeWeather data service 3.0"];
                        //   NSLog(@"list is %@",list);
                         NSDictionary * listDic = [list objectAtIndex:0];
                          // NSLog(@"listDic is %@",listDic);

                          NSArray * daily = [listDic objectForKey:@"daily_forecast"];
                          // NSLog(@"daily is %@",daily);

                          NSDictionary * dailyDic = [daily objectAtIndex:0];
                         //  NSLog(@"tmp is %@",dailyDic);

                           NSDictionary * tmpDic = [dailyDic objectForKey:@"tmp"];
                           NSLog(@"tmpDic is %@",tmpDic);

                           NSString * max = [tmpDic objectForKey:@"max"];
                           NSString * min = [tmpDic objectForKey:@"min"];

                           NSLog(@"max is %@",max);
                           NSLog(@"max is %@",min);

                       }
                   }];
}

img