2013-01-06 2 views
0

안녕하세요 저는 프로그래밍에 익숙하지 않습니다.Json SBJSON 오류 - NSDictionary에 대해 혼란 스럽습니다.

NSDictionary 및 표 셀에 몇 가지 문제가 있습니다. 좋은 자습서를 찾으려고했지만 나에게 도움이 될만한 것을 찾지 못했습니다. json을 구문 분석하기 위해 JSONKit을 사용하고 있습니다. 이전 버전의 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"]; 콘텐츠 = [결과 objectForKey : @ "n_name"]; 및에서 cell.textLabel.text = [content objectAtIndex : indexPath.row]; 배열로 처리합니다. 왜 문자열 "content"객체가 배열처럼 작동하는지 설명 할 수 있습니까? –

답변

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

문제의 세부 정보를 제공 할 수있어 문제를 해결할 수 있습니까? . –

+0

테이블에 데이터베이스 값을 가져올 수 없습니다. 오류가있을 수 있습니다 생각합니다 - cell.textLabel.text = [studName objectAtIndex : indexPath.row]; 데이터베이스 값을 가져 오지만 셀에 가져올 수 없습니다. –

관련 문제