如何使用NSJSONSerialization类参考读取JSON文件?

Elb*_*eth 6 json ios5

我需要使用NSJSONSerialization类参考读取JSON文件,我发现的有关此类使用的所有示例都从网页本身读取内容,而不是从之前创建的JSON文件中读取.任何人都知道如何使用该类从JSON文件解析?谢谢.

Dan*_*uke 13

简单,快速的例子:

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);
Run Code Online (Sandbox Code Playgroud)