NSError*error;
//获取文件路径NSString*filePath = [[NSBundlemainBundle]pathForResource:@"AppConfig"ofType:@"json"];
//根据文件路径读取数据NSData*jdata = [[NSDataalloc]initWithContentsOfFile:filePath];
//格式化成json数据
idjsonObject = [NSJSONSerializationJSONObjectWithData:jdataoptions:kNilOptionserror:&error];
附网上查到的读写方法:
将Json存进本地文件夹
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[paths objectAtIndex:0];
NSString *Json_path=[path stringByAppendingPathComponent:@"JsonFile.json"];
//==写入文件
NSLog(@"%@",[JsonData writeToFile:Json_path atomically:YES] ? @"Succeed":@"Failed");
-----------------------
读取Json
//==Json文件路径
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[paths objectAtIndex:0];
NSString *Json_path=[path stringByAppendingPathComponent:@"JsonFile.json"];
//==Json数据
NSData *data=[NSData dataWithContentsOfFile:Json_path];
//==JsonObject
id JsonObject=[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error]
//nslog (@"%@",jsonobject);打印json字典