我是 iOS 开发的新手。我想要做的是解析 JSON 网络服务(请参阅下面的示例输出)。我想将上述输出绑定到 UITableView。您能否向我展示如何执行此操作的示例代码。我正在使用带有 4.3 sdk 的 Xcode 3。非常感谢你!
[{
"event_id": "30",
"bar_name": "Area 05 Superclub",
"event_name": "test10",
"date": "Dec 05, 2012 10:00 AM"
}, {
"event_id": "27",
"bar_name": "Area 05 Superclub",
"event_name": "test7",
"date": "Dec 02, 2012 10:00 AM"
}, {
"event_id": "28",
"bar_name": "Area 05 Superclub",
"event_name": "test8",
"date": "Dec 03, 2012 10:00 AM"
}, {
"event_id": "29",
"bar_name": "Area 05 Superclub",
"event_name": "test9",
"date": "Dec 04, 2012 10:00 AM"
}]
好的,伙计们,这是可能的初始代码,(请注意 Tim Stullich,谢谢!)。我能够从网络服务中提取数据。我的下一个问题是如何将它绑定到 UITableView。希望你能再次帮助我。
-(void)loadData{
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http:xxx/gl_getEventsInformation.php"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];
// Each element in statuses is a single status
// represented as a NSDictionary
for (NSDictionary *status in statuses)
{ NSLog(@"%@ - %@ - %@ - %@", [status objectForKey:@"event_id"],[status objectForKey:@"bar_name"],[status objectForKey:@"event_name"],[status objectForKey:@"date"]);}
}