ios開發 json資料檔案的存取
阿新 • • 發佈:2019-02-15
將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字典
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字典