Objective-C 处理JSON的数据源

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

//初始化JSON解析器

    SBJSON *parser = [[SBJSON alloc] init];

//定义JSON数据的来源,例如http://localhost/MN_MemeberList.php

NSString*memberListUrl=[NSStringstringWithFormat:@"%@/MN_MemberList.php?act=%@",

[configgetBaseUrl],

@"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=[[NSArrayalloc]initWithArray:[parserobjectWithString:json_stringerror:nil]];

for(NSDictionary*memberinmemberList)

{

//NSLog(@"%@",[memberobjectForKey:@"username"]);

dataItem*item=[[dataItemalloc]init];

[itemsetFullName:[memberobjectForKey:@"username"]password:[memberobjectForKey:@"password"]overdue:[memberobjectForKey:@"overdue"]allow_edit:[memberobjectForKey:@"allow_edit"]];

[itemsaddObject:item];

[itemrelease];

}

[parserrelease];

[memberListrelease];

[json_stringrelease];

相关推荐