首页 > 代码库 > ios开发 json数据文件的存取

ios开发 json数据文件的存取

将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=http://www.mamicode.com/[NSData dataWithContentsOfFile:Json_path];
    //==JsonObject
 
    id JsonObject=[NSJSONSerialization JSONObjectWithData:data
                                                   options:NSJSONReadingAllowFragments
                                                     error:&error]
  //nslog (@"%@",jsonobject);打印json字典

ios开发 json数据文件的存取