1

我一直在查看堆栈,但无法让嵌套的 json 工作,

我不断收到此错误,并且我的数据未加载到表格视图中。如果我不使用嵌套,我的代码就可以工作。

2013-02-04 13:17:20.961 Big Wave App[2338:c07]-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 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;
}

任何帮助都会很棒,这是我的第一个 xcode 项目,对我来说是全新的。

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

4

1 回答 1

1

在您的项目中添加sbjson库。并尝试使用此代码而不是您的代码。在 .h 文件中

NSMUtableArray * idArray;

并在 .m 文件中

        #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];
}

更新

对于 tableview 添加这个

-(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;
}
于 2013-02-04T13:49:09.490 回答