一个留着自己看的json模版
json请求,回包处理,解析,缓存,发出通知刷新界面
【一个留着自己看的json模版】#头文件
#import
#import "ServerJsonPackage.h"
#import "MJExtension.h"#define kPrivilegeOperateInfoJson@"kPrivilegeOperateInfoJson"
#define kPrivilegeOperateInfoJsonFinish@"kPrivilegeOperateInfoJsonFinish"
#define PrivilegeOperateInfoJson_PACKAGE ((PrivilegeOperateInfoJson *)[ServerJsonPackage getPackage:kPrivilegeOperateInfoJson])
//json请求的数据模型
@interface PrivilegeOperateInfoModel : NSObject@property (nonatomic, assign)NSInteger ID;
@property (nonatomic, strong)NSString *name;
@property (nonatomic, assign)NSInteger value;
//通过字典获取模型
+(instancetype) PrivilegeOperateInfoModelWithDict:(NSDictionary *) dict;
//通过字典数组获取模型数组
+(NSArray *) PrivilegeOperateInfoModelWithDataArray:(NSArray*) data;
@end//json请求包
@interface PrivilegeOperateInfoJson : EasyServerJsonPackage
//注册json包,把这个类的一个对象以字典形式存入框架的json的proxy中,让宏PrivilegeOperateInfoJson_PACKAGE可以直接拿到这个json包的对象
//这个json包不是单利,每次通过这个宏获取对象,都是一个对象,相当于单例
+ (void)registerPackage;
//json包对外提供的方法,通过权限ID获取权限模型
-(PrivilegeOperateInfoModel *)getInfoModelWithId:(int)privilegeId;
@end
json请求,回包处理,解析,缓存,发出通知刷新界面
#点m文件
#import "PrivilegeOperateInfoJson.h"
#import "PPClient.h"
#import "Observer.h"
#import "ServerJsonPackage.h"
#import "ServerJsonPackageHolder.h"
#import "ServerField.h"
#import "ITalkOldUrlConfig.h"
#import "NSDataDisk.h"
#import "SimpleAPI.h"
//model解析#import "MJExtension.h"@implementation PrivilegeOperateInfoModel+(instancetype) PrivilegeOperateInfoModelWithDict:(NSDictionary *) dict
{
return [PrivilegeOperateInfoModel objectWithKeyValues:dict];
}
+(NSArray *) PrivilegeOperateInfoModelWithDataArray:(NSArray*) data
{
return [PrivilegeOperateInfoModel objectArrayWithKeyValuesArray:data];
}
- (NSDictionary *)replacedKeyFromPropertyName{
return @{
@"ID":@"id"
};
}
//用以归档的方法
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeInteger:self.ID forKey:@"ID"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.value forKey:@"value"];
}
//用以归档的方法
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.ID= [aDecoder decodeIntForKey:@"ID"];
self.name= [aDecoder decodeObjectForKey:@"name"];
self.value= https://www.it610.com/article/[aDecoder decodeIntForKey:@"value"];
}
return self;
}
@end@implementation PrivilegeOperateInfoJson
{
NSMutableArray * privilegeOperateInfoModelArr;
NSTimeInterval _mUpdateTimeInterval;
bool _haveLoad;
}+ (void)registerPackage
{
if (![SERVER_JSON_HOLDER getJsonPackage:kPrivilegeOperateInfoJson])
{
PrivilegeOperateInfoJson * jsonPackage = (PrivilegeOperateInfoJson *)[PrivilegeOperateInfoJson withDelegate:nil andJsonType:TYPE_SERVER_JSON_PACKAGE_DICTIONARY];
[SERVER_JSON_HOLDER registerJsonPackage:kPrivilegeOperateInfoJson withObject:jsonPackage];
}
}-(void)onSetPostData:(id)_httpRequestObject objectType:(NSString*)szType
{}-(ServerJsonRequestType)requestType
{
return SERVER_JSON_REQUEST_TYPE_GET;
}//返回是否能够发出请求
-(BOOL)canRequest
{
NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970];
if(nowTime - _mUpdateTimeInterval >10*60){
return YES;
}
return NO;
}-(NSString*)getHttpString
{
return PrivilegeOperateInfoJsonUrl;
}-(void)onRecvServerData:(id)_data withState:(NSInteger)kState
{
if (!_data)
{
ITLogSP(@"%@",@"data is nil");
return;
}
//mj直接转换为数组---练习代码>>>>>
NSArray * ppdataArr = [PrivilegeOperateInfoModel PrivilegeOperateInfoModelWithDataArray:_data];
//<<<<<<<<<<<<<<<<<练习代码
//mj字典转模型>>>>>>>
if (nil == privilegeOperateInfoModelArr) {
privilegeOperateInfoModelArr = [NSMutableArray array];
}
NSArray *dataArr = _data;
if (dataArr &&[dataArr isKindOfClass:[NSArray class]]) {//将现在时间存入userdefaults
_mUpdateTimeInterval = [[NSDate date] timeIntervalSince1970];
[NSUserDefaults setObject:@(_mUpdateTimeInterval) forKey:@"kPrivilegeOperateInfoJsonTime"];
[NSUserDefaults synchronize];
//将字典转换成模型存入数组privilegeOperateInfoModelArr
[privilegeOperateInfoModelArr removeAllObjects];
for (int i=0;
i
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 一个人的旅行,三亚
- 尽力
- 一个小故事,我的思考。
- 一个人的碎碎念
- 七年之痒之后
- 我从来不做坏事
- 异地恋中,逐渐适应一个人到底意味着什么()
- 牛人进化+|牛人进化+ 按自己的意愿过一生
- 别怪生活,自己作的!
- 迷失的世界(二十七)