第三方Json解析城市

使用第三方解析JsonKit  

禁止arc

//  ViewController.m

#import "JSONKit/JSONKit.h"

{

UITableView *tabel;

NSDictionary *dic;

}

#define JSON_URL @"http://127.0.0.1/1507C.json"

//====

//表格

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

tabel.delegate = self;

tabel.dataSource = self;

[self.view addSubview:tabel];

//字典

dic = [NSDictionary dictionary];

//

NSURL *url = [NSURL URLWithString:JSON_URL];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *respone, NSData *data, NSError *connectionElment)

{

NSLog(@"我的小可爱 == %@",respone.MIMEType);

if ([respone.MIMEType isEqualToString:@"application/json"])

{

dic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]copy];

}

}];

//========


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return dic.allKeys.count;

}

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

{

NSArray *as = [dic objectForKey:[dic.allKeys objectAtIndex:section]];

return as.count;

}

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

{

static NSString *cellid = @"cellid";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if (!cell)

{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];

}

NSArray *arr = [dic objectForKey:[dic.allKeys objectAtIndex:indexPath.section]];

cell.textLabel.text = [[arr objectAtIndex:indexPath.row]objectForKey:@"name"];

cell.detailTextLabel.text = [[arr objectAtIndex:indexPath.row]objectForKey:@"age"];

return cell;

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [dic.allKeys objectAtIndex:section];

}

你可能感兴趣的:(第三方Json解析城市)