iOS----解析网络json
来源:互联网 发布:手机自定义抽奖软件 编辑:程序博客网 时间:2024/05/22 01:34
一:当json以网址形式出现时:
(1)以前传统的解析json的方法:(代码存放位置是:在你需要解析的.m文件中)
NSError *error;
// 加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"这里是你的json的网址"]];
// 将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// iOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *Dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
// 字典中存放的数据也是字典型,从它里面通过键值取值
NSDictionary *Info = [Dic objectForKey:@“这里是你想要打印的json里边的某个值"];
// 打印出字典所存储数据
NSLog(@"%@",这里是你json想要打印出来的值);
(2)方法2:
// 加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://freegeoip.net/json/"]];
// 将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// iOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSString *ipDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
// weatherDic字典中存放的数据也是字典型,从它里面通过键值取值
NSDictionary *ipInfo = [ipDic objectForKey:@"ip"];
NSDictionary *ipInfo1=[ipDic objectForKey:@"country_code"];
NSDictionary *ipInfo2=[ipDic objectForKey:@"country_code"];
// 打印出weatherInfo字典所存储数据
NSDictionary *newDic=[ipDic objectFromJSONString];
NSLog(@"ipInfo字典里面的内容是--->%@",[newDic description]);
NSLog(@"ipInfo字典里面的内容是--->%@",[ipInfo1 description]);
NSLog(@"ipInfo字典里面的内容是--->%@",[ipDic objectForKey:@"country_code"]);
// 获取默认 Session
NSURLSession *session = [NSURLSession sharedSession];
// 创建 URL
NSURL *url = [NSURL URLWithString:@"http://freegeoip.net/json/"];
// 创建任务 task
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 获取数据后解析并输出
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
NSDictionary *info=[dic objectForKey:@"ip"];
NSLog(@"%@",[info description]);
NSDictionary *info2=[dic objectForKey:@"city"];
NSLog(@"%@",[info2 description]);
}];
// 启动任务
[task resume];
(2)方法2:
NSString *URLTmp1 =@"http:ip.taobao.com/service/getIpInfo.php?ip=myip";
NSString *URLTmp = [URLTmp1stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData * resData = [NSDatadataWithContentsOfURL:[NSURLURLWithString:URLTmp]];
if (resData) {
NSError *err;
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:resData
options:NSJSONReadingMutableContainers
error:&err];
NSLog(@"dic = %@", dic);
NSDictionary *datadic = [[NSDictionaryalloc] initWithDictionary:dic[@"data"]];
NSString *countryid = datadic[@"country_id"];
- iOS----解析网络json
- iOS开发网络 -- JSON解析
- iOS网络笔记--JSON解析
- IOS 网络解析(XML,JSON)
- iOS网络交互数据格式解析之json
- IOS学习笔记,网络请求,json解析
- iOS 网络数据请求和Json 解析
- iOS网络编程3—JSON解析
- 【iOS开发-网络】解析json数据
- iOS UI 14 网络解析json
- iOS 网络JSon解析使用框架
- ios网络数据下载和JSON解析
- iOS 网络编程 (四)JSON解析
- iOS开发网络篇 一一 JSON解析
- iOS开发: 网络解析数据之JSON解析
- iOS网络编程开发—JSON解析与XML解析
- iOS网络编程:网络交互数据格式解析之json
- IOS学习笔记(16)网络请求,json解析
- Python3学习(23)--__slots__ 限制实例属性的绑定
- Elasticsearch之文档。
- Spring Boot 实例通过接口安全退出
- hdu 6183 Color it(变种线段树)
- Server端判别客户端访问的客户端类型
- iOS----解析网络json
- Android动画效果之初识Property Animation(属性动画)
- 关于网页的400错误
- Azkaban的使用
- Split参数StringSplitOptions.RemoveEmptyEntries的使用
- 在数组中选择第i小的数
- 深度神经网络压缩
- 大话设计模式--简单工厂模式
- 磁珠在分割不同地中的作用以及如何放置