I’m trying to parse a JSON from this URL, which is a JSON output from WordPress. For some reason, there’s no output in the UITableView.
Here’s the code I have got to parse. I’m sure I’m missing out something but I’m not able to figure out how to parse Nested JSON in this code.
ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"News";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://www.karthikk.net/?json=1"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [news count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"date"];
return cell;
}
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UITableView *mainTableView;
NSArray *news;
NSMutableData *data;
}
@end
Please help me out in paring this.
Thanks much! I’ve tried referring to multiple threads on Stackoverflow, but all failed to work for me.
P.S.: I’m a newbie to iOS App Development. And I’m using this Video tutorial to mess with the code.
I see what the problem is. The news object is a
Dictionary
that has several key value pairs and one of them is posts which actually contains theArray
that you want to use to populate yourTableView
.Here is the first little bit from your
JSON
response:When you see a { in a
JSON
response that indicates aDictionary
, a [ indicates anArray
.TableViews
are populated from anArray
usually. So you can see that you have aJSON
response of aDictionary
with several key-value pairs and one of those is anArray
ofDictionary
‘sYou need to assign the news object the contents of that Dictionary key: