原生网络数据解析:
1、第一种方法:
NSURL *url =[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];
NSData *jsondata =[NSData dataWithContentsOfURL:url];
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments error:nil];
NSDictionary *dic =(NSDictionary*)jsonObj;
NSDictionary *dic1 =[dic objectForKey:@"weatherinfo"];
NSLog(@"%@", [dic1 objectForKey:@"city"]);
2、第二种方法:
NSURL *url =[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];
NSString *jsonStr =[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData *jsondata =[jsonStr dataUsingEncoding:NSUTF8StringEncoding];
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments error:nil];
NSDictionary *dic =(NSDictionary*)jsonObj;
NSDictionary *dic1 =[dic objectForKey:@"weatherinfo"];
NSLog(@"%@", [dic1 objectForKey:@"city"]);
3、第三种方法:
NSURL *url =[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
//参数1:请求数据 参数2:请求之后返回的信息 参数3:错误信息
NSData *jsondata = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//苹果已经放弃方法
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments error:nil];
NSDictionary *dic =(NSDictionary*)jsonObj;
NSDictionary *dic1 =[dic objectForKey:@"weatherinfo"];
NSLog(@"%@", [dic1 objectForKey:@"city"]);
最主要的是工程中别忘在plist文件中添加对http允许运行的代码
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

被折叠的 条评论
为什么被折叠?



