美文网首页
系统自带JSON解析

系统自带JSON解析

作者: Wang99 | 来源:发表于2017-10-14 08:40 被阅读0次

    手写JSON

    {
        "武侠":[
              {"MovieName":"叶问","MovieAuthor":"甄子丹","MoiveContent":"动作"},
              {"MovieName":"道士下山","MovieAuthor":"王宝强","MoiveContent":"动作"}
              ],
        
        "科幻":[
              {"MovieName":"长江七号","MovieAuthor":"周星驰","MoiveContent":"一件神奇的事情"}
              ],
        
        "爱情":[
              {"MovieName":"傲慢与偏见","MovieAuthor":"迪丽热巴","MoiveContent":"爱情"},
              {"MovieName":"分手合约","MovieAuthor":"彭于晏","MoiveContent":"爱情"},
              {"MovieName":"匆匆那年","MovieAuthor":"倪妮","MoiveContent":"爱情"}
              ]
        
    }
    
    
    
    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
        NSDictionary *dic;
        UITableView *table;
    }
    
    @end
    #define STUDENT_URL @"http://127.0.0.1/Movie.json"
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSURL *url = [NSURL URLWithString:STUDENT_URL];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
        
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        //系统自带的json解析
        dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"=======%@",dic);
        table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        table.dataSource = self;
        table.delegate = self;
        [self.view addSubview:table];
    }
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return dic.count;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        NSString *key = [dic.allKeys objectAtIndex:section];
        return [dic[key] count];
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSString *cellId = @"cellid";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        }
        
        NSString *key = [dic.allKeys objectAtIndex:indexPath.section];
        
        cell.textLabel.text =[[dic[key] objectAtIndex:indexPath.row] objectForKey:@"MovieName"];
        cell.detailTextLabel.text =[[dic[key] objectAtIndex:indexPath.row] objectForKey:@"MovieAuthor"];
        
        return cell;
        
    }
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return [dic.allKeys objectAtIndex:section];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    

    相关文章

      网友评论

          本文标题:系统自带JSON解析

          本文链接:https://www.haomeiwen.com/subject/hthxuxtx.html