基于MJE的数据处理封装
- .h文件
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger,SkyDataType) {
SkyDataTypeDictionary = 0,//最外层是字典
SkyDataTypeArray, //最外层是数组
SkyDataTypeNone //是字符串
};
@interface DataModelManager : NSObject
/**
处理data数据
@param responseObject 原始数据
@param dataType 数据最外层类型
@param modelName 模型名字
@param modelKey 解析关键字
@return 处理完的数据数组
*/
+(NSMutableArray *)dataDealWithResponseObject:(id)responseObject dataType:(SkyDataType)dataType modelName:(NSString *)modelName modelKey:(NSString *)modelKey;
@end
- .m文件
#import "DataModelManager.h"
#import "MJExtension.h"
@interface DataModelManager()
+(instancetype)sharedAFManager;
@end
@implementation DataModelManager
static DataModelManager *aManager;
+ (DataModelManager *)sharedAFManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
aManager = [[[super class] alloc]init];
});
return aManager;
}
+(NSMutableArray *)dataDealWithResponseObject:(id)responseObject dataType:(SkyDataType)dataType modelName:(NSString *)modelName modelKey:(NSString *)modelKey
{
//处理二进制数据
NSString *biteObject = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
//过滤无用字符
biteObject = [[self sharedAFManager] filtrationString:biteObject deleteStringArray:@[@"\r\n",@"\n",@"\t"]];
//整理
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if (!skyStringIsEmpty(biteObject)) {
if (dataType == SkyDataTypeNone) {
//是字符串
dic[modelName] = @[biteObject];
}
if (dataType == SkyDataTypeDictionary) {
NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
//最外层是字典
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
[dic addEntriesFromDictionary:dict];
}
if (dataType == SkyDataTypeArray) {
NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
//最外层是数组
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
dic[modelName] = array;
}
}
//解析
NSMutableArray *modelArray = [NSMutableArray array];
//是字符串
if (dataType == SkyDataTypeNone) {
[modelArray addObjectsFromArray:dic[modelName]];
}
//最外层是字典
if (dataType == SkyDataTypeDictionary) {
[modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelKey]]];
}
//最外层是数组
if (dataType == SkyDataTypeArray) {
[modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelName]]];
}
return modelArray;
}
#pragma mark --- 去除字符
-(NSString *)filtrationString:(NSString *)String deleteStringArray:(NSArray *)deleteStringArray
{
if (String == NULL || [String isEqualToString:@""]) {
return @"";
}
NSMutableString *str1 = [NSMutableString stringWithString:String];
for (int i = 0; i<deleteStringArray.count; i++) {
NSRange range = [str1 rangeOfString:deleteStringArray[i]];
while (range.location != NSNotFound) {
[str1 deleteCharactersInRange:range];
range = [str1 rangeOfString:deleteStringArray[i]];
}
}
return str1;
}
@end
主要步骤
创建DataModelManager类,继承自NSObject类
数据类型用枚举定义
typedef NS_ENUM(NSInteger,SkyDataType) {
SkyDataTypeDictionary = 0,//最外层是字典
SkyDataTypeArray, //最外层是数组
SkyDataTypeNone //是字符串
};
- 在.m文件中导入#import "MJExtension.h",并获取DataModelManager类単例
@interface DataModelManager()
+(instancetype)sharedAFManager;
@end
@implementation DataModelManager
static DataModelManager *aManager;
+ (DataModelManager *)sharedAFManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
aManager = [[[super class] alloc]init];
});
return aManager;
}
@end
- 将元数据转二进制数据并过滤无用字符
//处理二进制数据
NSString *biteObject = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
//过滤无用字符
biteObject = [[self sharedAFManager] filtrationString:biteObject deleteStringArray:@[@"\r\n",@"\n",@"\t"]];
- 去除字符方法实现
#pragma mark --- 去除字符
-(NSString *)filtrationString:(NSString *)String deleteStringArray:(NSArray *)deleteStringArray
{
if (String == NULL || [String isEqualToString:@""]) {
return @"";
}
NSMutableString *str1 = [NSMutableString stringWithString:String];
for (int i = 0; i<deleteStringArray.count; i++) {
NSRange range = [str1 rangeOfString:deleteStringArray[i]];
while (range.location != NSNotFound) {
[str1 deleteCharactersInRange:range];
range = [str1 rangeOfString:deleteStringArray[i]];
}
}
return str1;
}
- 整理过滤完的数据
//整理
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if (!skyStringIsEmpty(biteObject)) {
if (dataType == SkyDataTypeNone) {
//是字符串
dic[modelName] = @[biteObject];
}
if (dataType == SkyDataTypeDictionary) {
NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
//最外层是字典
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
[dic addEntriesFromDictionary:dict];
}
if (dataType == SkyDataTypeArray) {
NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
//最外层是数组
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
dic[modelName] = array;
}
}
- 使用mj_objectArrayWithKeyValuesArray解析
//解析
NSMutableArray *modelArray = [NSMutableArray array];
//是字符串
if (dataType == SkyDataTypeNone) {
[modelArray addObjectsFromArray:dic[modelName]];
}
//最外层是字典
if (dataType == SkyDataTypeDictionary) {
[modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelKey]]];
}
//最外层是数组
if (dataType == SkyDataTypeArray) {
[modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelName]]];
}
- 以上