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);

Output NSLog list return

(null)

my apps use json file internal my project. and "th.json" in my project folder. but NSArray *list can't display data in Json file.

user1913291
  • 1
  • 1
  • 3

2 Answers2

1

You need to give the content of the file to the SBJsonParser. Your code just gives the path to the file to the parser. Try this instead:

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);
Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39
  • Why build error: No visible @interface for 'SBJson4Parser' declares the selector 'objectWithData:' – Gank Nov 06 '14 at 09:24
  • 1
    SBJson4Parser is from SBJson version 4 and up, SBJsonParser (which is used in this example) is in SBJson versions *less than* 4. This is a MAJOR VERSION CHANGE because the API changed significantly. Therefore EVERYTHING was renamed so you can use both version 3 and 4 in the same application. – Stig Brautaset Nov 07 '14 at 18:24
0
for (NSDictionary *dataDict in myitems) {
        NSString *itemName = [dataDict objectForKey:@"itemName"];
        NSLog(@"itemName = %@",itemName);
    }    

insert String make it work.

user1913291
  • 1
  • 1
  • 3