0

我正在做一个原生 objC 应用程序。ios5 我想解析本地包中的 JSON 文件。并单独阅读每个项目。接下来是我的 JSON 文件的示例,然后是我正在工作的代码。我还解释了我认为正在发生的事情,以及我需要获得帮助的示例代码。也欢迎您纠正我的解释。

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

{"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":"X","C1":"NR"," CABG":"NR","PCI":"NR"}}, {"b":{"Q01":"A01","Q02":"B01","Q03":"C01","Q04" :"D01","Q05":"E05","Q06":"X","C1":"NR","CABG":"NR","PCI":"NR"}}NR"}}, {"b":{"Q01":"A01","Q02":"B01","Q03":"C01","Q04":"D01","Q05":"E04" ,"Q06":"X","C1":"NR","CABG":"NR","PCI":"NR"}}, {"b":{"Q01":"A01"," Q02":"B01","Q03":"C01","Q04":"D01","Q05":"E05","Q06":"X","C1":"NR","CABG" :"NR","PCI":"NR"}}NR"}}, {"b":{"Q01":"A01","Q02":"B01","Q03":"C01","Q04":"D01","Q05":"E04" ,"Q06":"X","C1":"NR","CABG":"NR","PCI":"NR"}}, {"b":{"Q01":"A01"," Q02":"B01","Q03":"C01","Q04":"D01","Q05":"E05","Q06":"X","C1":"NR","CABG" :"NR","PCI":"NR"}}"CABG":"NR","PCI":"NR"}}, {"b":{"Q01":"A01","Q02":"B01","Q03":"C01","Q04 ":"D01","Q05":"E05","Q06":"X","C1":"NR","CABG":"NR","PCI":"NR"}}"CABG":"NR","PCI":"NR"}}, {"b":{"Q01":"A01","Q02":"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”的第三个 B NSDictionary 值吗

谢谢

4

1 回答 1

0

[[[root objectForKey:@"a"] valueForKey:@"b"] objectAtIndex:2]

于 2012-05-01T20:52:04.250 回答