0

I am new to iOS, please help me how can I read a local JSON file using objective-c

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

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"auth_request" ofType:@"json"];


NSError *error = nil;

NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
NSString *json_string = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];



NSArray *statuses = [parser objectWithString:json_string error:nil];



for (NSDictionary *status in statuses) {


    NSLog(@"%@ - %@", [status objectForKey:@"loginId"], [status objectForKey:@"secret"]);
}
2
  • First thing to learn, it doesn't matter that you are using xcode you could be doing this in a text editor and it would still be the same. Commented Jan 6, 2014 at 20:27
  • 2
    NSArray* statuses = [NSJSONSerialization JSONObjectFromData:JSONData options:0 error:&error]; (where error is the usual NSError object). Commented Jan 6, 2014 at 21:06

1 Answer 1

-1

Just Add NSArray *tempArray = [json_string JSONVALUE] in the place of NSArray *statuses = [parser objectWithString:json_string error:nil];

Don't forget to import

Sign up to request clarification or add additional context in comments.

4 Comments

which file i have to import
Don't start a newbie on a 3rd-party JSON library. He should be using Apple's NSJSONSerialization unless there's a good reason to do otherwise.
could u please tell me how to parse json file with code or send me some good links
@user3156748 - I explained how to do it 19 minutes ago. See my comment below your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.