Wednesday, 14 August 2013

Reading JSON array into UITableView on iOS app?

Reading JSON array into UITableView on iOS app?

Here is my JSON:
[{
"type": "Game",
"platform": "PC ",
"title": "The Elder Scrolls V: Skyrim",
"link": "\/game\/pc\/the-elder-scrolls-v-skyrim",
"metaScore": "94",
"criticReview": "favorable",
"releaseDate": "Nov 11, 2011 ",
"rating": "M ",
"publisher": "Bethesda Softworks ",
"description": "The next chapter in the Elder Scrolls saga arrives
from the Bethesda Game Studios. Skyrim reimagines the open-world
fantasy epic, bringing to life a complete virtual world open for you
to explore... "
}, {
"type": "Game",
"platform": "PC ",
"title": "The Elder Scrolls V: Skyrim - Dragonborn",
"link": "\/game\/pc\/the-elder-scrolls-v-skyrim---dragonborn",
"metaScore": "83",
"criticReview": "favorable",
"releaseDate": "Feb 5, 2013 ",
"rating": "M ",
"publisher": "Bethesda Softworks ",
"description": "With this official add-on for The Elder Scrolls V:
Skyrim, journey off the coast of Morrowind, to the island of
Solstheim. Encounter new towns, dungeons, and quests, as you traverse
the ash wastes... "
}, {
"type": "Game",
"platform": "X360 ",
"title": "The Elder Scrolls V: Skyrim",
"link": "\/game\/xbox-360\/the-elder-scrolls-v-skyrim",
"metaScore": "96",
"criticReview": "favorable",
"releaseDate": "Nov 11, 2011 ",
"rating": "M ",
"publisher": "Bethesda Softworks ",
"description": "The next chapter in the Elder Scrolls saga arrives
from the Bethesda Game Studios. Skyrim reimagines the open-world
fantasy epic, bringing to life a complete virtual world open for you
to explore... "
}
This is the code in my ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UITableView *mainTableView;
NSArray *news;
NSMutableData *data;
}
@end
And here is the ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"News";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL
URLWithString:@"http://jackstonedev.com/external/metasearchjson.php?query=skyrim"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (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;
NSArray *responseDict = [NSJSONSerialization JSONObjectWithData:data
options:nil error:nil];
news = [responseDict objectAtIndex:0];
[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:@"metaScore"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row]
objectForKey:@"title"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
As far as I can tell, the code that puts the JSON into the dictionary is
wrong, as it is trying to find an index which does not exist. Is there a
way to change this code so that it can read an array of JSON into the
dictionary instead?

No comments:

Post a Comment