私はネイティブの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」の 3 番目の B NSDictionary 値を表示するコードを教えてください。
ありがとう