SBJson 框架使用2 SBJson解析数据 解析字典,数组
先导入SBJSON框架 导入sbjson 头文件 #import "SBJson.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; dataArray = [[NSMutableArray alloc] init]; NSString* str = [NSString stringWithFormat:@"http://192.168.88.8/sns/my/user_list.php"]; NSURL* url = [NSURL URLWithString:str]; //下载 str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; //解析 NSDictionary* dic = [str JSONValue]; //得到用户数组 NSArray* usersArray = [dic objectForKey:@"users"];
for (NSDictionary* userDic in usersArray) { //取得用户昵称 NSString* username = [userDic objectForKey:@"username"]; //取得用户头像url NSString* headimage = [NSString stringWithFormat:@"http://192.168.88.8/sns%@",[userDic objectForKey:@"headimage"]]; //把用户昵称和头像url保存到字典中 NSMutableDictionary* tmpDic = [NSMutableDictionary dictionaryWithCapacity:0]; [tmpDic setObject:username forKey:@"username"]; [tmpDic setObject:headimage forKey:@"headimage"]; //把字典存到表格数据源的数组中 [dataArray addObject:tmpDic]; } _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,320,460) style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; [_tableView release]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return dataArray.count; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ID"] autorelease]; } //取出对应行的用户信息 NSDictionary* dic = [dataArray objectAtIndex:indexPath.row]; //用户昵称 cell.textLabel.text = [dic objectForKey:@"username"]; //用户头像url NSString* imageUrl = [dic objectForKey:@"headimage"]; //下载用户头像 NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]; //显示用户头像 cell.imageView.image = [UIImage imageWithData:data]; return cell; } - (void)dealloc{ [dataArray release]; [super dealloc]; } @end
要解析的文本内容 { "totalcount": "2532","count": 20,"users": [ { "uid": "2492","username": "cccxxx","groupid": "5","credit": "105","experience": "95","viewnum": "3","friendnum": "0","lastactivity": 0,"headimage": "/my/headimage.php?uid=2492","realname": "" },{ "uid": "2415","username": "baiyunhui","groupid": "6","credit": "150","experience": "135","viewnum": "11","headimage": "/my/headimage.php?uid=2415",{ "uid": "2412","username": "heiseqinjian","credit": "163","experience": "149","viewnum": "92","friendnum": "1","headimage": "/my/headimage.php?uid=2412",{ "uid": "2403","username": "yao","credit": "129","experience": "111","viewnum": "1","headimage": "/my/headimage.php?uid=2403",{ "uid": "2288","username": "1314","credit": "128","experience": "118","viewnum": "14","headimage": "/my/headimage.php?uid=2288",{ "uid": "2429","username": "Qulili","credit": "122","experience": "112","viewnum": "79","headimage": "/my/headimage.php?uid=2429",{ "uid": "1944","username": "xyf9575","credit": "177","experience": "167","viewnum": "13","headimage": "/my/headimage.php?uid=1944",{ "uid": "1946","username": "haizi123869","credit": "199","experience": "184","viewnum": "4","friendnum": "4","headimage": "/my/headimage.php?uid=1946",{ "uid": "2409","username": "gfy10010","credit": "87","experience": "77","viewnum": "0","headimage": "/my/headimage.php?uid=2409",{ "uid": "2411","username": "shin","experience": "136","viewnum": "27","friendnum": "3","headimage": "/my/headimage.php?uid=2411","realname": "u563bu563b" },{ "uid": "2388","username": "1307010","credit": "135","experience": "115","headimage": "/my/headimage.php?uid=2388",{ "uid": "2390","username": "1303004","credit": "96","experience": "142","headimage": "/my/headimage.php?uid=2390",{ "uid": "1194","username": "clover","groupid": "7","credit": "3592","experience": "3288","viewnum": "59947","friendnum": "46","headimage": "/my/headimage.php?uid=1194","realname": "clover" },{ "uid": "2525","username": "u4e1bu6797u5c0fu98deu732a","credit": "21","experience": "27","viewnum": "10","headimage": "/my/headimage.php?uid=2525",{ "uid": "2117","username": "u2196u7eafu771fu2197","credit": "26","experience": "644","viewnum": "3720","friendnum": "7","headimage": "/my/headimage.php?uid=2117",{ "uid": "907","username": "thlcm","credit": "144","experience": "134","viewnum": "207","headimage": "/my/headimage.php?uid=907",{ "uid": "2524","username": "u841du535cu5154","credit": "67","experience": "37","viewnum": "2","headimage": "/my/headimage.php?uid=2524",{ "uid": "2459","username": "u6e05u98ce","credit": "77","experience": "100","viewnum": "25","headimage": "/my/headimage.php?uid=2459",{ "uid": "2439","username": "Dome19890502","credit": "94","experience": "79","viewnum": "7","headimage": "/my/headimage.php?uid=2439",{ "uid": "1919","username": "u88abu59b3u7761u670d","credit": "47","experience": "929","viewnum": "21495","friendnum": "16","headimage": "/my/headimage.php?uid=1919","realname": "u6241u5634u4f26" } ] } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |