plist文件与json的存储与读取
2015-11-30 19:40
387 查看
</pre><pre name="code" class="objc"> NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //得到完整的文件名 NSString *filename=[plistPath stringByAppendingPathComponent:@"searchHistory.plist"]; NSMutableArray *historyArr = [[[NSMutableArray alloc]initWithContentsOfFile:filename]mutableCopy]; //NSMutableDictionary *historyArr = [[[NSMutableDictionary alloc]initWithContentsOfFile:filename]mutableCopy]; // NSMutableArray *historyArr = [applist objectForKey:@"history"]; if (!historyArr) { historyArr = [NSMutableArray arrayWithCapacity:0]; }//如果数组里没有,就把搜索关键字存进去 if ([historyArr indexOfObject:searchBar.text] == NSNotFound) { [historyArr addObject:searchBar.text]; // NSMutableArray *data=[[NSMutableArray alloc]init]; //[data setObject:historyArr forKey:@"history"]; //[data writeToFile:filename atomically:YES]; [historyArr writeToFile:filename atomically:YES]; }; <pre name="code" class="objc">将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字典
相关文章推荐
- 大鱼吃小鱼游戏-collision.js
- 解决JS中各浏览器Date格式不兼容的问题
- 大鱼吃小鱼游戏-baby.js
- 大鱼吃小鱼游戏-mom.js
- 大鱼吃小鱼游戏-ane.js
- servlet与jsp的区别(转)
- fastjson文档解读——下篇
- js弹出层
- JSON的序列化与还原
- js传参乱码问题
- JSP 最佳实践: 用 jsp:include 控制动态内容
- JSP 最佳实践: 使用JSTL来更新JSP页面
- JSP 最佳实践: 使用 JSP include 机制改进外观
- $.each遍历json对象
- jsp servlet的区别和联系(转)
- JSP 技术 —— 是敌是友?
- JSP 技术 -- 是友还是敌?
- Angular JS 学习笔记(一)
- js判断移动设备或者pc
- js特效之最近的两个星期天