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": "\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" } ] }

相关文章

  jsonp需要在页面中添加一个<script>元素,由该元素来从其他服务器加载json数据。 <body&g...
<script> var testApi = "地址"; $.ajax({ url:testApi,//可以不是本地域名 type:‘post...
总是有人会遇到跨域问题,然后有个jsonp的解决方案,MVC中代码如下: public class JsonpResult : Syst...
最近开发中遇到调用第三方web_api的功能,后端在处理json数据时使用fastjson来做反序列化,由于调用api...
JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读、编写、解析。jsoncpp...
JsonSerializer有多个属性,用于自定义如何序列化JSON。这些也可以通过JsonSerializerSettings参数,在...