2010-08-12 57 views
2

我想解析使用JSON解析器和結果,我得到我必須把它放到表視圖。我已經傳遞了一個常量鍵值和一個字符串。JSON解析器輸出顯示在tableview

是否存在解析步驟錯誤?或錯過了。

我已經包含了JSON解析器的代碼。

在此先感謝。

SBJSON *parser = [[SBJSON alloc] init]; 

NSString *urlString =[NSString stringWithFormat:@"http://api.shopwiki.com/api/search?key=%@&q=%@",apiKey, string]; 
NSURL *url = [NSURL URLWithString:urlString]; 
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url]; 
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

NSMutableArray *statuses = [[NSMutableArray alloc]init]; 
statuses = [parser objectWithString:json_string error:nil]; 
NSLog(@"Array Contents: %@", statuses); 



NSMutableArray *statuses0 = [[NSMutableArray alloc]init]; 
statuses0 = [statuses valueForKey:@"offers"]; 
NSLog(@"Array Contents: %@", statuses0); 


//For Title 
NSMutableArray *statuses1 = [[NSMutableArray alloc]init]; 
statuses1 = [[[statuses valueForKey:@"offers"] valueForKey:@"offer"]valueForKey:@"title"]; 
NSLog(@"Array Contents 4 Title: %@", statuses1); 

在這裏,在statuses1陣列我得到20個對象裏面全是冠軍,現在我只是想顯示的標題爲實現代碼如下: -

的實現代碼如下代碼片段: -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
NSLog(@"status count:%d",[statuses1 count]); 
return [statuses1 count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSLog(@"Inside Tableview"); 
int counter=indexPath.row; 

NSString *CellIdentifier = [NSString stringWithFormat:@"%d",counter]; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
    { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.selectionStyle=UITableViewCellSelectionStyleNone; 


} 

NSLog(@"Inside Tableview 1"); 

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

NSLog(@"Inside Tableview 2"); 

return cell; 
} 

我得到不好的超額異常,它擊中: -

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

請給我解決方案 在此先感謝: -

+0

這是一樣的http://stackoverflow.com/questions/3461494/json-parsing-error 幾個編輯? – 2010-08-12 16:05:56

+0

是的,但我已經編輯了它的一些部分.. – 2010-08-12 16:11:03

回答

0

如果您在該行上獲得EXC_BAD_ACCESS,可能是因爲statuses1已在發佈時間cellForRowAtIndexPath被調用。這一行代碼是什麼?

NSMutableArray *statuses1 = [[NSMutableArray alloc]init]; 

以上statuses1變量是本地的你已經宣佈它在什麼範圍之內。你把它分配給你的UITableViewController.statuses1伊娃?這是一個保留的財產?