ios json 解析本地数据

第三方

pod 'SDWebImage'

pod 'AFNetworking'


文件夹

model.h

继承NSObject

@property(nonatomic , strong)NSString *imageV;

@property(nonatomic , strong)NSString *nameText;

@property(nonatomic , strong)NSString *detailText;


文件夹

Controller

#import "image.h"

#import "AFNetworking.h"

#import "AFHTTPSessionManager.h"

@interface imageViewController ()

@property (nonatomic,strong) UITableView * table;

@property (nonatomic,strong) NSMutableArray * arr;

@end

@implementationimageViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //_arr = [NSMutableArray new];




    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"  ‍  ➕" style:UIBarButtonItemStyleDone target:self action:@selector(yy)];


    self.navigationController.navigationBar.barTintColor=[UIColor blackColor];

    _table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

    _table.delegate=self;

    _table.dataSource=self;


    [self.view addSubview:_table];

}

-(void)yy{


    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/PY.json"];


    NSData *data = [NSData dataWithContentsOfURL:url];

    //解析json数据  将json转换为oc对象  序列化

    NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    //一层一层的解析

//    NSDictionary *plistDic = dict1[@"plist"];

//    NSDictionary *dictDic = plistDic[@"dict"];

//    NSArray *studentArr = dictDic[@"student"];


    //NSLog(@"%@",dict1);

    //初始化可变数组

    //self.arr = [NSMutableArray array];

    //for in 解析

    self.arr=[NSMutableArray array];


    NSArray*arr = dict1[@"dict"];

    for(NSDictionary* diinarr) {

        image*model = [[imagealloc]init];

        [modelsetValuesForKeysWithDictionary:di];

        //[self.arr addObject:model];

        [self.arraddObject:model];

    }




        //刷新

    [self.table reloadData];





}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arr.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    }


    image*model=self.arr[indexPath.row];

    NSData *dataimg=[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imageV]];

    cell.imageView.image=[UIImage imageWithData:dataimg];

    cell.textLabel.text=model.nameText;

    cell.detailTextLabel.text=model.detailText;


    returncell;

}

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{

    return 130;

}

你可能感兴趣的:(ios json 解析本地数据)