IOS解析JSON

初学者最近自学IOS,今天试了一下json解析,从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization)

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    NSError *error;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html" ]];
    
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response  options:NSJSONReadingMutableLeaves error:&error];
    
    NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
    Weather.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n ", [weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]; ;
    
    NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
    
    
    
}
text显示的时候碰见内容一行显示解决方案
  1. 调整Label属性“Lines”为“6”
  2. 调整Label属性“Line breaks”为“Word Wrap”
参考: http://my.oschina.net/shootercn/blog/102084


IOS解析JSON

你可能感兴趣的:(ios,Object-C)