2012-05-01 3 views
0

의있는 NSDictionary에서 값하고있는 중이 지. ios5 로컬 번들에서 JSON 파일을 구문 분석하려고합니다. 그리고 각 항목을 개별적으로 읽으십시오. 따라야 할 것은 JSON 파일의 샘플이며 그 다음에 작업중인 코드가 나와 있습니다. 나는 또한 내가 무슨 일을하고 있는지에 대한 설명과 함께 도움을 받아야하는 샘플 코드를 가지고있다. 내 해석을 고칠 수 있습니다.나는 기본 objC 응용 프로그램을 NSDictionaries

============================================== =============

{ "a": [ { "b": { "Q01": "A01 ","Q02 ":"B01 ","Q03 ":"C01 ","Q04 ":"D01 ","Q05 ":"E01 ","Q06 ":"X ","C1 ":"NR " "CABG": "NR", "PCI": "NR"}} { "B"{ "Q01": "A01", "Q02": "B01", "Q03": "C01", " 「Q04」: 「D01」 「Q05」 「E02」 「Q06」 「X」 「C1」 「I」 「C1IND」 「20」 「C1SCORE」: 「3」 「CABG」 "NR", "PCI": "NR"}} { "B"{ "Q01": "A01", "Q02": "B01", "Q03": "C01", "Q04" "D01", "Q05": "E03", "Q06": "X", "C1": "NR", "CABG": "NR", "PCI": "NR"}} { "B ":"{Q01 ":"A01 ","Q02 ":"B01 ","Q03 ":"C01 ","Q04 ":"D01 ","Q05 ":"E04 ","Q06 " "C1": "NR", "CABG": "NR", "PCI"{ "Q01": "A01", "Q02"{ "B", "NR"}} "B01" "Q03": "C01" , "Q04": "D01", "Q05": "E05", "Q06": "X", "C1": "NR", "CABG": "NR", "PCI": "NR" ============================

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"nonacs" ofType:@"json"];  

NSError* error = nil; 
NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error]; 


if (jsonData) { 

    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 

    if (error) { 
     NSLog(@"error is %@", [error localizedDescription]); 
     // Handle Error and return 
     return; 
    } 

    //NSArray *keys = [jsonObjects allKeys];  
    // not necessary becuase I know the Root key is a single "a" 

    NSString* jsonString = [NSString stringWithFormat:@"%@",[jsonObjects objectForKey:@"a"]]; 


    tLabel01.text = [NSString stringWithFormat:@"jsonData Length is = %d", [jsonData length]]; 

    NSDictionary* dict01 = [[NSDictionary alloc] initWithDictionary:jsonObjects]; 

    l2.text = [NSString stringWithFormat:@"dict01 length = %d", dict01.count]; 


    NSDictionary* dict02 =[[NSDictionary alloc] initWithDictionary:dict01]; 
    l3.text = [NSString stringWithFormat:@"dict2.length = %d", dict02.count]; 


    //NSArray* array01 = [[NSArray alloc] initWithObjects:dict01, nil]; 

    //NSArray *allvalues = [jsonObjects allValues];  
    //NSArray *allvalues = [[jsonObjects initWithContentsOf ] allValues]; 


    //l3.text = [NSString stringWithFormat:@"allvalues.count = %d", allvalues.count];   

    //NSDictionary *dictB = [dict01 objectForKey:@"b"]; 


    //l3.text = [NSString stringWithFormat:@"dictB.count = %d", dictB.count]; 

    l3.text = [NSString stringWithFormat:@"jsonObject count = %d", [[jsonObjects objectForKey:@"a"] count]]; 
    // this works really well... except it returned 1700, and there should only be 1550 

    //l4.text = @"nada"; 
    //[[jsonObjects objectForKey:@"a"] count]]   

    NSArray *array = [jsonObjects objectForKey:@"a"]; 
    NSArray *arrayVariable = [array objectAtIndex:0]; 

    //l4.text = [NSString stringWithFormat:@"Q01 = %@", [arrayVariable objectForKey: @"Q01"]];   

    //l4.text =[NSString stringWithFormat:@"arrayVariable count = %d", arrayVariable.count]; 

    NSArray *arrayb = [jsonObjects objectForKey:@"b"];   
    l4.text = [NSString stringWithFormat:@"arrayb count = %d", arrayb.count]; 





    bTV01.text = jsonString; 


    }; 

을 :

은]}

내 코드처럼 보인다 ===================================

루트 개체는 단일 키 "a"를 가진 NSDictionary입니다. 해당 값에 해당하는 값은 NSArray입니다. 배열에 단일 키 "b"가있는 NSDictionaries가 있습니다. B NSDictionaries에는 다른 NSDictionary가 포함되어 있습니다.

누군가가 나에게 "Q02"에 대한

하시기 바랍니다 감사

을 3 B NSDictionary에 값을 표시하는 코드를 보여줄 수

답변

0

[[[루트 objectForKey : @ "A"] valueForKey : "B"@] objectAtIndex : 2]

관련 문제