libchief
libchief

Reputation: 21

Trouble on parsing JSON and displaying it on UITableView

I have this url: http://maps.google.com.br/maps/api/directions/json?origin=porto+alegre&destination=novo+hamburgo&sensor=false

And this code:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

NSMutableDictionary *theDictionary = [responseString JSONValue];

NSLog(@"%@", theDictionary);

[responseString release];
if (theDictionary != nil) 
{
    NSArray *routesArray = [theDictionary objectForKey:@"routes"];
    NSDictionary *firstRoute = [routesArray objectAtIndex:0];
    NSArray *legsArray = [firstRoute objectForKey:@"legs"];
    NSDictionary *firstLeg = [legsArray objectAtIndex:0];
    steps = [firstLeg objectForKey:@"steps"];
}

[tableView reloadData];

}

I want to display every single @"html_instructions" on a uitableview.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.steps count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"];

if (cell == nil)
    cell = [[[UITableViewCell alloc] 
             initWithFrame:CGRectZero reuseIdentifier:@"cachedCell"] autorelease];

return cell;

}

What's missing in my code??? Should be some little detail... any help would be GREAT!

Upvotes: 1

Views: 510

Answers (2)

Yanchi
Yanchi

Reputation: 1030

I would do it like this: Create url request with your url

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
        [request setURL:[NSURL URLWithString:@"http://maps.google.com.br/maps/api/directions/json?origin=porto+alegre&destination=novo+hamburgo&sensor=false"]];

then create NSData structure, that holds response to the request and I converted it to string

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //Or async request
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

After that, you load JSON into Dictionary, then to move one level down, pass it to another dictionary (result 1)

NSError *error=nil;
    result = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
    result1 = [result objectForKey:@"routes"];
    result2 = [result1 objectForKey:@"legs"];

Then move it into NSMutableArray

jsonMutArray= [result2 objectForKey:@"steps"];

From there, you just put label into your custom cell attach it as a IBOutlet, synthetize and do in cellForRowAtIndexPath method

cell.yourLabel.text = [jsonMutArray objectForKey:@"html_instructions"];

However, keep in mind, that I'm just begginer, so this might not be the most effective way of doing it :) Good Luck!

Upvotes: 1

KMG
KMG

Reputation: 93

Don't you need to be doing something like this in your CellForRowAtIndexPath ?

NSString *cellValue = **WHATEVER VALUE YOU WANT TO PUT IN**;

cell.textLabel.text = cellValue;

return cell;

I don't see you adding your data to the cell.

Upvotes: 0

Related Questions