iOS 读取.json文件内容

在编码过程中,遇到了如何获取和使用.json文件中的内容:
下面为大家提供一种方法:

    NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];
    NSString *path=[mainBundleDirectory stringByAppendingPathComponent:@".json文件名"];
    NSURL *url=[NSURL fileURLWithPath:path];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"%@",dic);

打印的dic就是.json文件的内容,根据内容进行使用。

希望可以帮助大家
如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议
iOS技术交流群:668562416

你可能感兴趣的:(iOS 读取.json文件内容)