//
// ViewController.m
// MyJson
//
// Created by Providence Enterprise Limited on 14-10-18.
// Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//
#import "ViewController.h"
#import "SBJson/SBJson.h"
//#import "TouchJson/JSON/CJSONDeserializer.h"
@interface ViewController (){
NSDictionary *myweatherInfo;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)touchJsonButton:(id)sender {
//不支持arc
}
//使用SBJson解析南阳的天气
- (IBAction)sbJsonButton:(id)sender {
/* ================ 多线程学习 ========================== */
//新建线程,处理解析
[NSThread detachNewThreadSelector:@selector(sbJsonFunc) toTarget:self withObject:nil];
//operation
NSInvocationOperation *invOpertion=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sbJsonFunc) object:self];
NSOperationQueue *queue=[[NSOperationQueue alloc] init];
[queue addOperation:invOpertion];
//GCD
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 处理耗时操作的代码块...
[self sbJsonFunc];
//通知主线程刷新
dispatch_async(dispatch_get_main_queue(), ^{
//回调或者说是通知主线程刷新,
[self updateUI:myweatherInfo];
});
});
}
-(void)sbJsonFunc{
// NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];
// NSError *error = nil;
// NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
// SBJsonParser *parser = [[SBJsonParser alloc] init];
//
// NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];
// myweatherInfo = [rootDic objectForKey:@"weatherinfo"];
// //新建刷新主界面
//// [self performSelectorOnMainThread:@selector(updateUI:)withObject:myweatherInfo waitUntilDone:YES];
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
myweatherInfo = [weatherDic objectForKey:@"weatherinfo"];
}
//刷新主界面函数
-(void)updateUI:(NSDictionary *)weatherInfo{
weatherInfo=myweatherInfo;
// self.txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather6"], [weatherInfo objectForKey:@"temp1"]];
self.txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
}
- (IBAction)IOS5JsonButton:(id)sender {
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
self.txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
}
- (IBAction)JsonKitButton:(id)sender {
//不支持arc
}