当前位置: 代码迷 >> JavaScript >> Objective-C 处置JSON的数据源
  详细解决方案

Objective-C 处置JSON的数据源

热度:594   发布时间:2012-10-16 09:57:37.0
Objective-C 处理JSON的数据源

// 注意! 需要在工程中添加JSON.framework
??? items = [[NSMutableArray alloc] init];


??? // 初始化JSON解析器
?? ?SBJSON *parser = [[SBJSON alloc] init];


// 定义JSON数据的来源,例如 http://localhost/MN_MemeberList.php
?? ?NSString *memberListUrl = [NSString stringWithFormat:@"%@/MN_MemberList.php?act=%@",
?? ??? ??? ??? ??? ??? ??? ??? [config getBaseUrl],
?? ??? ??? ??? ??? ??? ??? ??? @"null"
?? ?];
?? ?NSLog(@"%@",memberListUrl);
?? ?NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:memberListUrl]];


// 把url的结果返回给response
?? ?NSData *response = [NSURLConnection sendSynchronousRequest:request

? ? returningResponse:nil error:nil];


// 取得JSON数据的字符串
?? ?NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];


// 把JSON转为数组
?? ?NSArray *memberList = [[NSArray alloc] initWithArray:[parser objectWithString:json_string error:nil]];
?? ?
?? ?for(NSDictionary *member in memberList)
?? ?{
?? ??? ?//NSLog(@"%@",[member objectForKey:@"username"]);
?? ??? ?dataItem *item = [[dataItem alloc] init];
?? ??? ?[item setFullName:[member objectForKey:@"username"] password:[member objectForKey:@"password"] overdue:[member objectForKey:@"overdue"] allow_edit:[member objectForKey:@"allow_edit"]];
?? ??? ?[items addObject:item];
?? ??? ?[item release];
?? ?}
?? ?
?? ?[parser release];
?? ?[memberList release];
?? ?[json_string release];

  相关解决方案