2012-07-06 5 views
0

웹 서비스에서 json을 구문 분석하는 첫 번째 iOS 앱을 빌드하려고합니다. 내 .H 파일에서iOS에서 json 문자열 구문 분석 목표 C

은 내가 웹 사이트를 호출하고 배열에 데이터를 저장 내하는 .m 파일에서 배열

NSArray *items; 

을 만들 수 있습니다. 이 모든 것은 결국 나는 uitableview에 데이터를 표시하려고 노력하고있다.

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

    static NSString *CellIdentifier = @"ItemsCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    NSLog(@"%@",items); 

    NSDictionary *item = [items objectAtIndex:indexPath.row]; 
    NSString *title = [item objectForKey:@"title"]; 

    cell.title.text = title; 

    return cell; 
} 

항목의 nslog는 생산 :

{ 
category = code; 
keyword = ""; 
limit = 20; 
lowPrice = ""; 
page = 0; 
products =  (
    { 
     description = "..."; 
     mobileFriendly = 1; 
     popularity = 2021; 
     productId = code; 
     title = "title"; 
    }, and so on... 
) 

내가 NSDictionary에 일을하려고 오류를 얻고있다 * 항목 = [항목 objectAtIndex : indexPath.row] 그것은 objectAtIndex : indexPath.row를 수행하는 것이 올바르지 않다고 말합니다.

내가 뭘 잘못하고 있니?

도움 주셔서 감사합니다.

답변

0

items에 저장된 값은 NSDictionary의 인스턴스이지만, NSArray 메시지를 보내고 있습니다. 또한 게시 한 코드가 gift을 나타내며 어쨌든 항목을 무시합니다.

실제로 items 사전 내부 키 products 아래에 저장됩니다 찾고있는 배열, 그래서 당신은 같은 것을 할 필요가 : 내가있는 NSDictionary * 항목으로 헤더 파일을 변경하면 너무

NSArray *products = [items objectForKey:@"products"]; 

NSDictionary *product = [products objectAtIndex:indexPath.row]; 
// Then use the product, instead of using 'gift' (whatever that is). 
NSString *title = [product objectForKey:@"title"]; 
+0

감사합니다. – Stephen

0

항목은 NSArray이 아닙니다. 이것은 NSDictionary입니다. 그래서 그것은 objectAtIndexPath: 방법이 없습니다.

+0

을 ... 제품에 액세스하려면 어떻게해야합니까? 감사! – Stephen