I did try to bring JSON data to the iPhone and connected printed the incoming JSON data using NSLOG .was able to print and test the particular node.The i started using a for-loop to loop through the Array of feed and display it in a UItableview and was not able to to do so . Tried a number of options but could not sort out this one .Please take a look at the code and let me know where and what i am doing wrong.I am still learning iPhone coding so any inout would be highly helpful.please find the code below.
#import "RootViewController.h"
#import "SBJson.h"
#import "JSON.h"
@implementation RootViewController
NSMutableArray *listOfStates;
NSArray *exercises;
NSMutableData *responseData;
- (void)viewDidLoad
{
//---initialize the array---
listOfStates = [[NSMutableArray alloc] init];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/~xxxx/MyWorks/index.php?playerid=0"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
//NSURL *url = [NSURL URLWithString:@""];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"didReceiveResponse"); [responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data]; }
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [dictionary valueForKey:@"feed"];
NSLog(@"Here is the title of the feed: %@", [response valueForKey:@"video"]);
exercises = [[NSArray alloc] initWithArray:response];
[[self tableView] reloadData];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [exercises count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [dictionary valueForKey:@"feed"];
//create a cell
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
// fill it with contnets
int ndx;
for (ndx = 0; ndx <dictionary.count; ndx++) {
NSDictionary *player = (NSDictionary *)[dictionary objectAtIndex:ndx];
NSLog(@"Player: %@", [player valueForKey:@"player"]);
}
NSString *cellValue = [player objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// return it
return cell;
}
It's hard to say what's wrong without knowing what is happening, but here are a couple of observations.
Based on the JSON you added in your comment, it appears that the exercises array will contain NSDictionary objects. That means your code to set the cell text is not right. Instead of
NSString *cellValue = [response objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
you probably want something like
NSDictionary *exercise = [response objectAtIndex:indexPath.row];
cell.textLabel.text = [exercise valueForKey"player"];