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

前端 未结 1 1926
醉酒成梦
醉酒成梦 2021-02-09 15:07

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 i

相关标签:
1条回答
  • 2021-02-09 15:46

    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);
    
    0 讨论(0)
提交回复
热议问题