• Json解析


    TouchJson、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法,解析需要下载他们的库

    首先建立一个新的工程,(注意不要选择ARC机制)添加如下控件:

    1. #import <UIKit/UIKit.h>  
    2.   
    3. @interface ViewController : UIViewController  
    4.   
    5. @property (retain, nonatomic) IBOutlet UITextView *txtView;  
    6.   
    7. - (IBAction)btnPressTouchJson:(id)sender;  
    8. - (IBAction)btnPressSBJson:(id)sender;  
    9. - (IBAction)btnPressIOS5Json:(id)sender;  
    10. - (IBAction)btnPressJsonKit:(id)sender;  
     
    1. //使用TouchJson来解析北京的天气  
    2. - (IBAction)btnPressTouchJson:(id)sender {  
    3.     //获取API接口  
    4.     NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];  
    5.     //定义一个NSError对象,用于捕获错误信息  
    6.     NSError *error;  
    7.     NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];  
    8.     NSLog(@"jsonString--->%@",jsonString);  
    9.     //将解析得到的内容存放字典中,编码格式为UTF8,防止取值的时候发生乱码  
    10.     NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error];  
    11.     //因为返回的Json文件有两层,去第二层内容放到字典中去  
    12.     NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];  
    13.     NSLog(@"weatherInfo--->%@",weatherInfo);  
    14.     //取值打印  
    15.     txtView.text = [NSString stringWithFormat:@"今天是 %@  %@  %@  的天气状况是:%@  %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];  
    16.       
    17. }
    1. //使用SBJson解析南阳的天气  
    2. - (IBAction)btnPressSBJson:(id)sender {  
    3.     NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];  
    4.     NSError *error = nil;  
    5.     NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];  
    6.     SBJsonParser *parser = [[SBJsonParser alloc] init];  
    7.       
    8.     NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];  
    9.     NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];  
    10.     txtView.text = [NSString stringWithFormat:@"今天是 %@  %@  %@  的天气状况是:%@  %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
    1. - (IBAction)btnPressIOS5Json:(id)sender {  
    2.       
    3.     NSError *error;  
    4.     //加载一个NSURL对象  
    5.     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]];  
    6.     //将请求的url数据放到NSData对象中  
    7.     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];  
    8.     //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中  
    9.     NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];  
    10.     NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];  
    11.     txtView.text = [NSString stringWithFormat:@"今天是 %@  %@  %@  的天气状况是:%@  %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];  
    12.     NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );  
  • 相关阅读:
    网页布局
    Block Formatting Context
    SEO初识
    新的一个月,就这么不知不觉的来临了
    Js结束,项目进行中
    JS学习中....
    ws快捷键
    Html的学习以及webstorm的使用
    从事前端开发应该了解的CSS原理
    jQuery动画
  • 原文地址:https://www.cnblogs.com/tianlianghong/p/5317473.html
Copyright © 2020-2023  润新知