Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a JSON file using the NSJSONSerialization Class Reference?

Tags:

json

ios5

I need to read a JSON file using the NSJSONSerialization Class Reference, and all the examples that I have found about the use of this class read the content from the webpage itself, instead of reading from a JSON file that has been previously created.Anyone knows how to parse from a JSON file using that class? Thanks.

like image 201
Elbereth Avatar asked May 14 '12 11:05

Elbereth


1 Answers

Simple, quick'n'dirty example:

NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"foobar"
                                                     ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data
                                          options:kNilOptions
                                            error:&error];
NSLog(@"JSON: %@", json);
like image 150
Daniel Bauke Avatar answered Oct 19 '22 18:10

Daniel Bauke