這是給定的API,我必須解析。我還必須在我的UITableView
上顯示名稱。我想獲取名稱的值n在表格視圖中顯示它。 這是代碼:JSON解析:在UITableView上顯示數據
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [ episodes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [[ episodes objectAtIndex:indexPath.row] objectForKey:@"names"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *logoutConfirm = [[UIAlertView alloc]initWithTitle:@"Toilet Finder" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Display on Map",@"Route",@"Cancel", nil];
logoutConfirm.tag = 111;
[logoutConfirm show];
}
- (void) parseJSONWithURL:(NSURL *) jsonURL
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
dispatch_async(queue, ^{
NSError *error = nil;
NSString *json = [NSString stringWithContentsOfURL:jsonURL
encoding:NSASCIIStringEncoding error:&error];
if (error == nil){
[email protected]"http://api.kivaws.org/v1/loans/search.json?status=fundraising";
NSLog(@"URL %@", WebInServe);
NSURL *Final_Url = [NSURL URLWithString:WebInServe];
NSData* jsonData = [NSData dataWithContentsOfURL: Final_Url];
NSError* error;
jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSLog(@"What is contain let me check %@",jsonDict);
NSArray* latestLoans = [jsonDict objectForKey:@"loans"];
NSLog(@"loans in array: %@", latestLoans);
episodes = [[jsonDict valueForKey:@"latestLoans"]valueForKey:@"name"];
if (error == nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
episodes = [[jsonDict valueForKey:@"latestLoans"]valueForKey:@"name"];
[tableView reloadData];
});
}
我已經嘗試使用:
episodes = [[jsonDict valueForKey:@"latestLoans"]objectForKey:@"name"];
我已經使用幾個tymes objectForKey
和valueForkey
但在上面的代碼,並在
cell.textLabel.text = [[ episodes objectAtIndex:indexPath.row] objectForKey:@"names"];
線的代碼cellForRowAtIndexPath: method
但數據不會顯示在tableview中......事實上,這是顯示在NSLog
...
檢查本教程:http://www.raywenderlich.com/5492/working-with-json-in-ios-5 –