美文网首页
xcode加载json文件的方法

xcode加载json文件的方法

作者: 雾霭天涯 | 来源:发表于2021-12-24 10:17 被阅读0次

加载json文件的方法

NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];
NSString *path=[mainBundleDirectory stringByAppendingPathComponent:@"test.json"];
NSURL *url=[NSURL fileURLWithPath:path];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

或者

// 读取本地JSON文件
- (NSDictionary *)readBundleJson{
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"appserver" ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
}

test.json文件中的内容布局如下,注意json文件的数据格式不能错误,否则获取内容为nil

[
    [
        2804181,
        3010030,
        3004134,
        3721160,
        3718524,
        3715941
    ]
    ,
    [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
    ]
]

相关文章

网友评论

      本文标题:xcode加载json文件的方法

      本文链接:https://www.haomeiwen.com/subject/barlqrtx.html