2013-01-06 32 views
0

您好我是新來的節目。的Json SBJSON錯誤 - 困惑的NSDictionary

我有一個NSDictionary中和表單元格的一些問題。我試圖找到很好的教程,但沒有找到任何可以幫助我的東西。我正在使用JSONKit來解析json。我有一箇舊版本的xcode,只有模擬器4.3。

什麼,我想要做的就是我的Json的解析與單元格的表。

我想解析JSON:

{[{"n_id":"1","name":"Green","age":"23"}]} 

我的主要問題。 我沒有得到任何錯誤,但是當我運行應用程序時,它只是自動關閉。 當我註釋掉//cell.textLabel.text = [studName objectAtIndex:indexPath.row];並用cell.textLabel.text = @「Test」替換它。該應用程序和輸出工作正常。我也能夠得到NSLog();值

我想我的錯誤是在cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 在此先感謝

//Json2ViewController.m

- (void)viewDidLoad { 
[super viewDidLoad]; 



NSData * JSONdata =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://myurl.php"]]; 

if([JSONdata length] == 0){ 
[JSONdata release]; 
return; 
} 

NSArray *students =[JSONdata objectFromJSONData]; 

studName = [[NSMutableArray alloc] init]; 

for(NSDictionary *student in students) 
{ 
    NSLog(@"Name: %@", [student objectForKey:@"name"]); 
    NSLog(@"Age: %@", [student objectForKey:@"age"]); 

    content = [student objectForKey:@"name"]; 
    if(content) 
     [studName addObject:content]; 

} 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 


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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell... 
cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 
return cell; 

}

我不知道有關的NSMutableArray。

//Json2ViewController.h

@interface Json2ViewController : UIViewController { 
NSArray * list; 
NSString * content; 
NSMutableArray *studName; 

}

+0

到底是什麼你的問題?你有什麼問題與NSDictionary?你是否期待不同的價值? – TeaPow

+0

您提取代碼中的字符串部分 // NSString * content = [results objectForKey:@「n_name」]; content = [result objectForKey:@「n_name」]; 和在 cell.textLabel.text = [內容objectAtIndex:indexPath.row]; 你把它當作一個數組來處理。 你能解釋爲何以及如何一個字符串「內容」對象的行爲像一個數組 –

回答

1

試試這個:

//Declare NSMutableArray *studName= [[NSMutableArray alloc] init]; in your .h file. 
// 3. iterate the array; each element is a dictionary... 


for (NSDictionary *results in list) 
    { 
     // 3 ...that contains a string for the key "stunde" 
     content = [results objectForKey:@"n_name"]; 
     if(content) 
      [studName addObject:content]; 
    } 

,並在索引行的表委託方法細胞

// Configure the cell... 
      cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 
+0

嗨,我剛剛改變了代碼,但我仍然得到一些錯誤 –

+0

u能請給這個問題的細節出現,這樣我可以解決這個問題。 –

+0

我無法將數據庫值存入我的表中。我認爲錯誤可能是 - cell.textLabel.text = [studName objectAtIndex:indexPath.row];我確實得到了數據庫值,但我無法將它們放入單元格中。 –