1

I've been looking through stack and I cant get nested json to work,

I keep getting this error and my data is not being loaded into the table view. My code works if i dont used nested.

2013-02-04 13:17:20.961 Big Wave App[2338:c07] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7165510

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

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"http://blog.bigwavemedia.co.uk/?json=get_recent_posts"];
    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;

    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" 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:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }

    cell.textLabel.text = [[[news objectAtIndex:indexPath.row] objectForKey:@"title"] valueForKey:@"id"];

    return cell;
}

Any help would be great this is my first xcode project, all new to me.

http://jsonviewer.stack.hu/#http://blog.bigwavemedia.co.uk/?json=get_recent_postshttp://blog.bigwavemedia.co.uk/?json=get_recent_posts

7
  • You're trying to use an object as an array whereas it's a dictionary. Check your logic and the JSON. Commented Feb 4, 2013 at 13:29
  • Can you help me? what do i need to change? Commented Feb 4, 2013 at 13:33
  • what value from your response you want to display in tableview? Commented Feb 4, 2013 at 13:35
  • @BrentFrench I don't know - you haven't provided the JSON data nor the relevan code (the code you listed is unrelated to the error). You have to ensure that an array is really an array and not a dictionary - analyse the structure of your JSON by reading the specs on json.org. Commented Feb 4, 2013 at 13:35
  • jsonviewer.stack.hu/#http://blog.bigwavemedia.co.uk/… Here is the Json, and I want to display the post ID. Thanks Commented Feb 4, 2013 at 13:39

1 Answer 1

1

add sbjson library in your project.and try to using this code instead of yours. in .h file

NSMUtableArray * idArray;

and in .m file

        #import "SBJson.h"

- (void)viewDidLoad
{       

idArray = [[NSMutableArray alloc] init];

 NSString *post =@"";

                NSURL *url=[NSURL URLWithString:@"http://blog.bigwavemedia.co.uk/?json=get_recent_posts"];

                NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

                NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
                [request setURL:url];
                [request setHTTPMethod:@"POST"];
                [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
                //[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
                // [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                [request setHTTPBody:postData];

                //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

                NSError *error = [[NSError alloc] init];
                NSHTTPURLResponse *response = nil;
                NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];



                //NSLog(@"Response code: %d", [response statusCode]);
                if ([response statusCode] >=200 && [response statusCode] <300)
                {
                    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                    // NSLog(@"Response ==> %@", responseData);                    

                    SBJsonParser *jsonParser = [SBJsonParser new];
                    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];


                    //NSLog(@"json data is : %@",jsonData);

                    NSArray *allDataArray = [[NSArray alloc]init];
                    allDataArray= [jsonData objectForKey:@"posts"];


                    for(int i=0;i<[allDataArray count];i++)
                    {
                        NSDictionary *tempDictionary = [allDataArray objectAtIndex:i];


                        if([tempDictionary objectForKey:@"id"]!=nil)
                        {

                            [idArray addObject:[tempDictionary objectForKey:@"id"]];
                        }

                    }

                } else {
                    if (error) NSLog(@"Error: %@", error);
                    [self alertStatus:@"Connection Failed" :@"Does not find any data!"];
                }


            }
            @catch (NSException * e) {
                NSLog(@"Exception: %@", e);
                [self alertStatus:@"Data not found." :@"Does not find any data!"];
            }
[super viewDidLoad];
}

UPDATE

And for tableview add this

-(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:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }

    cell.textLabel.text = [idArray objectAtIndex:indexPath.row];

    return cell;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks so much for the reply, So i add this all in the viewDidLoad part or my m?
and dont forget to add SBJson library
I think i did everything correct and iv got errors can i zip up project so you can take a look. I will tick you up as soon as its fixed
www.bigwavemedia.co.uk/App.zip
can i get your mail address bcoz i dont have server to upload.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.