Json   发布时间:2019-10-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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


要解析的文本内容

{ "@R_502_10586@lcount": "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": "\u563b\u563b" },{ "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": "\u4e1b\u6797\u5c0f\u98de\u732a","credit": "21","experience": "27","viewnum": "10","headimage": "\/my\/headimage.php?uid=2525",{ "uid": "2117","username": "\u2196\u7eaf\u771f\u2197","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": "\u841d\u535c\u5154","credit": "67","experience": "37","viewnum": "2","headimage": "\/my\/headimage.php?uid=2524",{ "uid": "2459","username": "\u6e05\u98ce","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": "\u88ab\u59b3\u7761\u670d","credit": "47","experience": "929","viewnum": "21495","friendnum": "16","headimage": "\/my\/headimage.php?uid=1919","realname": "\u6241\u5634\u4f26" } ] }

大佬总结

以上是大佬教程为你收集整理的SBJson 框架使用2 SBJson解析数据 解析字典,数组全部内容,希望文章能够帮你解决SBJson 框架使用2 SBJson解析数据 解析字典,数组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。