您的位置:首页 > Web前端 > JavaScript

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字典



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: