第三方Json解析城市

使用第三方解析JsonKit  

禁止arc

//  ViewController.m

#import "JSONKit/JSONKit.h"

<UITableViewDelegate,UITableViewDataSource>

{

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];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容