我正在做一个原生的objC应用程序。 iOS5的 我想解析本地包中的JSON文件。 并单独阅读每个项目。 下面是我的JSON文件的示例,后面是我工作的代码。 我也解释了我的想法,以及我需要获得帮助的示例代码。我们也欢迎你纠正我的解释。
=============================================== ==========================
{ “一”:[ { “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”}}
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值的代码吗
感谢
答案 0 :(得分:0)
[[[root objectForKey:@“a”] valueForKey:@“b”] objectAtIndex:2]