基于jsonkit,缓存(缓存对象 是服务器返回的整个json字符串)工具类,
2013-03-14 10:27
369 查看
#import "CacheUtil.h"
#import "JSONKit.h"
@implementation CacheUtil
//获得访问目录
+ (NSString*)documentPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
//写缓存
+ (void)writeCache:(id)json ofType:(NSString *)type
{
NSString* content = [json JSONString];
[content writeToFile:[[CacheUtildocumentPath] stringByAppendingPathComponent:type] atomically:YESencoding:NSUTF8StringEncodingerror:nil];
}
//读缓存
+ (id)readCacheOfType:(NSString *)type
{
NSString* path = [[CacheUtildocumentPath] stringByAppendingPathComponent:type];
if (![[NSFileManagerdefaultManager] fileExistsAtPath:path]) {
return nil;
}
NSError* error;
NSData* content = [[NSData alloc] initWithContentsOfFile:path];
if (error) {
return nil;
}
id json = [content objectFromJSONData];
return json;
}
//清除缓存
+ (void)clearCacheOfType:(NSString*)type
{
NSString* path = [[CacheUtildocumentPath] stringByAppendingPathComponent:type];
if ([[NSFileManagerdefaultManager] fileExistsAtPath:path]) {
[[NSFileManagerdefaultManager] removeItemAtPath:path error:nil];
}
}
@end
#import "JSONKit.h"
@implementation CacheUtil
//获得访问目录
+ (NSString*)documentPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
//写缓存
+ (void)writeCache:(id)json ofType:(NSString *)type
{
NSString* content = [json JSONString];
[content writeToFile:[[CacheUtildocumentPath] stringByAppendingPathComponent:type] atomically:YESencoding:NSUTF8StringEncodingerror:nil];
}
//读缓存
+ (id)readCacheOfType:(NSString *)type
{
NSString* path = [[CacheUtildocumentPath] stringByAppendingPathComponent:type];
if (![[NSFileManagerdefaultManager] fileExistsAtPath:path]) {
return nil;
}
NSError* error;
NSData* content = [[NSData alloc] initWithContentsOfFile:path];
if (error) {
return nil;
}
id json = [content objectFromJSONData];
return json;
}
//清除缓存
+ (void)clearCacheOfType:(NSString*)type
{
NSString* path = [[CacheUtildocumentPath] stringByAppendingPathComponent:type];
if ([[NSFileManagerdefaultManager] fileExistsAtPath:path]) {
[[NSFileManagerdefaultManager] removeItemAtPath:path error:nil];
}
}
@end
相关文章推荐
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象[修订081217]
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 serlet
- struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)
- jquery中怎样把字符串解析成json【前端$.parseJSON(字符串)返回json对象详细看文章内容】
- post发送JSON数据(字符串、数组、字典、自定义对象)给服务器
- 服务器返回的JSON字符串
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 serlet配置页面
- 服务器端返回的JSON字符串 怎样在 JS中转换成可访问的 JSON对象
- PHP学习 - 返回的json对象变成了字符串
- 工作总结 返回 json 后台对象中,某一个字段设为string 整个对象转换为json 返回到页面中
- java将JSON字符串转换为实体类对象,基于net.sf.json实现
- 如何使用Retrofit获取服务器返回来的JSON字符串
- 如何利用fastjson将JSON格式的字符串转换为Map,再返回至前端成为js对象
- java 对象与json字符串互相转换工具类
- Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加
- $.ajax传递字符串到后台,后台返回json对象
- 不通过struts2,直接将Json对象转为字符串返回到jsp页面
- ajax返回json字符串转换为JSON对象
- 将一个对象转化为类json风格字符串的工具类
- Json返回通用对象,工具类