Daniel Hansson
Daniel Hansson

Reputation: 75

Parse JSON to Tableview in a viewcontroller

Im trying to parse a json to my tableview, inside a viewController. But when Im trying to Log it it will not appear. Also I wonder how to put this JSON into my tableviewcells.

I changed my code since I first put up this question.

Please see my code below:

.m:

#import "UbaGuideStartViewController.h"

#import <QuartzCore/QuartzCore.h>





@interface UbaGuideStartViewController (){
    NSMutableData *jsonData;
    NSURLConnection *connection;
    NSMutableArray *arrData;
}


@property (weak, nonatomic) IBOutlet UIImageView *ImgHeader;


@property (weak, nonatomic) IBOutlet UIImageView *ImgTitle;



@end

@implementation UbaGuideStartViewController



 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [[self StartTableView]setDelegate:self];
    [[self StartTableView]setDataSource:self];
    arrData = [[NSMutableArray alloc]init];



}

//Json parse.

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse  *)response
{
    [jsonData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [jsonData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Failed with error");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{
    NSDictionary *guideDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

    NSArray *arrguideTitle = [guideDictionary objectForKey:@"guide"];

    for (NSDictionary *dictionary in arrguideTitle)
    {
        NSString *guideTitle = [dictionary objectForKey: @"guideTitle"];

        [arrData addObject:guideTitle];
    }
    [self.StartTableView reloadData];

    NSURL *url = [NSURL   URLWithString:@"http://www.sofisto.com.br/public_html/TEST/guide.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];

    if (connection){
        jsonData = [[NSMutableArray alloc]init];
    }
}


//TableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *startGuideCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (startGuideCell == nil) {
        startGuideCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // configure your cell here...

    startGuideCell.textLabel.text = [arrData objectAtIndex:indexPath.row];



    return startGuideCell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

Also my JSON:

{
"guide": [
{ "guideTitle":"Where to stay"}, 
{ "guideTitle":"Where to eat"}, 
{ "guideTitle":"What to do"}
]
}

Thanks in advance!

Upvotes: 1

Views: 1452

Answers (2)

cocoahero
cocoahero

Reputation: 1302

Not sure if this is intentional or not, but you're creating and starting the connection from within the connectionDidFinishLoading callback.

Try starting your connection (with the below code) in your viewDidAppear or similar lifecycle callback method.

NSURL *url = [NSURL   URLWithString:@"http://www.sofisto.com.br/public_html/TEST/guide.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

connection = [NSURLConnection connectionWithRequest:request delegate:self];

if (connection){
    jsonData = [[NSMutableArray alloc]init];
}

Upvotes: 0

jpcguy89
jpcguy89

Reputation: 217

You need to use NSJSONSerializer to decode the JSON to either an NSArray or NSDictionary, depending on what's stored in the JSON. For instance:

NSError *error;
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

In my case, the JSON is series of dictionaries. This code takes the dictionaries and stores them into an array. I can grab one:

NSDictionary *dict = [jsonArray objectAtIndex:0;

And then use that dictionary;

NSString *string = [dict objectForKey:@"someKey"];

So essentially, you can use the data to populate your cells just like any other object you would use.

Upvotes: 3

Related Questions