0

th.json

{"lessons":[{"id":"38","fach":"D"},{"id":"39","fach":"M"}]}

ViewController.m

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *JsonData = [bundle pathForResource:@"th" ofType:@"json"];

SBJsonParser *parser = [[SBJsonParser alloc] init] ;

NSDictionary *jsonObject = [parser objectWithString:JsonData error:NULL];

NSArray *list = [jsonObject objectForKey:@"lessons"];

NSLog(@"%@", list);

出力 NSLog リスト リターン

(null)

私のアプリは私のプロジェクト内でjsonファイルを使用します。プロジェクトフォルダーの「th.json」。ただし、NSArray *list は Json ファイルのデータを表示できません。

4

2 に答える 2

1

ファイルの内容を SBJsonParserに渡す必要があります。あなたのコードは、ファイルへのパスをパーサーに与えるだけです。代わりにこれを試してください:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"th" ofType:@"json"];
NSLog(@"Path: %@", path);

NSData *jsonData = [NSData dataWithContentsOfFile:path];
NSLog(@"Data: %@", jsonData);

SBJsonParser *parser = [[SBJsonParser alloc] init] ;

NSDictionary *jsonObject = [parser objectWithData:jsonData];
NSLog(@"%@", jsonObject);

NSArray *list = [jsonObject objectForKey:@"lessons"];
NSLog(@"%@", list);
于 2012-12-27T16:23:42.433 に答える
0
for (NSDictionary *dataDict in myitems) {
        NSString *itemName = [dataDict objectForKey:@"itemName"];
        NSLog(@"itemName = %@",itemName);
    }    

文字列を挿入して機能させます。

于 2012-12-19T04:47:57.340 に答える