有网址解析网络数据

先看一下效果图:

                


这里是已知网址http://api.milltary.app887.com/api/Articles.action?opc=10&npc=0&type=%E6%9C%80%E6%96%B0%E9%B2%9C,当然也可以是抓来的接口。

言归正传:主要实现代码如下:

//解析文件
- (void)configTableData{
      //加载一个NSuRL对象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.milltary.app887.com/api/Articles.action?opc=10&npc=0&type=%E6%9C%80%E6%96%B0%E9%B2%9C"]];
    //将请求的url数据放到NSData中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:nil];
    //打开document路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",documentPath);
    //第二步:创建存储字典的plist文件的存储路径
    NSString *dicPath = [documentPath stringByAppendingPathComponent:@"dic.plist"];
    //第三步:写入
    [dic writeToFile:dicPath atomically:YES];
    //读取
    NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    //解析文件
    self.allFourArray = [NSMutableArray new];
    NSArray *array = readDic.allKeys;
    for (NSString *key in array) {
        self.dic1 = [dic objectForKey:key];
        }
    self.arrays = self.dic1[@"list"];
    self.array = [NSMutableArray new];
    for (NSDictionary *dict in self.arrays) {
        [self.array addObject:dict[@"url"]];
        FourModel *fourModel = [[FourModel alloc] initWithDictionary:dict];
       [self.allFourArray addObject:fourModel];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //点中某一行实现的方法
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.array[indexPath.row]]];
}

 在刚开始的时候遇到bug,是因为存入沙河路径,强制转换成了NSString类型,可是有属性的类型是Number类型。所以读取的时候要用字符串拼接的方法去解决这一问题。    类型前后一定要保持一致!!!!!!!!


从图片可以看出,工程中代码实现图片的时候,使用:

//图片   图片需要在.h中导入框架
    [self.imagelink setImageWithURL:[NSURL URLWithString:model.imglink] placeholderImage:nil];

导入的框架是:#import <UIImageView+WebCache.h>










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值