模型->jsonString
 方式1.
 NSDictionary *paramDict = getTakeupInfo.yy_modelToJSONObject;
 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:paramDict/*getTakeupInfo.mj_keyValues*/
                                                    options:NSJSONWritingPrettyPrinted
                                                      error:nil ];
 NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
 方式2 [queryCalendarInfo yy_modelToJSONString];

模型->字典
[outputInfo yy_modelToJSONObject];

 jsonString -> 模型
 id param = [outputInfo.D yy_modelToJSONObject];
 AddEventCallbackInfo *addEventCallbackInfo = [AddEventCallbackInfo yy_modelWithDictionary:param];
 id paramDict = [inputInfo yy_modelToJSONObject];
 InputInfo *inputInfo = [InputInfo yy_modelWithDictionary:parameters];

 NSData *jsonData = [(NSString *)self.eventInfo.su dataUsingEncoding : NSUTF8StringEncoding];
 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
 InputInfo *inputInfo = [InputInfo yy_modelWithDictionary:dict];

NSString *str = userTakeupInfo.P;
NSData *jsonData = [(NSString *)str dataUsingEncoding : NSUTF8StringEncoding];
ContactInfo *contactInfo = nil;
if(jsonData){
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
    contactInfo = [ContactInfo yy_modelWithDictionary:dict];
}


 jsonString ->模型数组
 NSArray *array = [NSArray yy_modelArrayWithClass:[UserTakeupInfo class] json:outputInfo.D];
 NSMutableArray *userTakeupInfoArray = [NSMutableArray arrayWithArray:array];
 
 模型数组 -> JsonString
 [contactInfoArray yy_modelToJSONString]

 模型数组->字典数组
id param = [corpDomainArray yy_modelToJSONObject];

 
 json字典数组-> 字典数组
 NSData *jsonData = [(NSString *)dataJson dataUsingEncoding : NSUTF8StringEncoding];
 NSArray *dictDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
 


#import <YYModel/YYModel.h>
 
 + (NSDictionary *)modelContainerPropertyGenericClass {
     return @{@"p" : [PageInputInfo class]
             };
 }
 
TokenLoginOutServerInfo 中的IPList无法进行转换对应属性 ,手动再次转换了一次
NSData *jsonData = [(NSString *)serverInfo dataUsingEncoding : NSUTF8StringEncoding];
NSDictionary *param = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
//                id param = [serverInfo yy_modelToJSONObject];
TokenLoginOutServerInfo *outServerInfo = [TokenLoginOutServerInfo yy_modelWithDictionary:param];
NSArray *array = [NSArray yy_modelArrayWithClass:[TokenLoginOutServerIPInfo class] json:[param objectForKey:@"IPList"]];
outServerInfo.IPList = array;


 
 //返回一个 Dict,将 Model 属性名对映射到 JSON 的 Key。
 + (NSDictionary *)modelCustomPropertyMapper {
     return @{@"name" : @"n",
              @"page" : @"p",
              @"desc" : @"ext.desc",
              @"bookID" : @[@"id",@"ID",@"book_id"]};
 }
 
 // 字典转模型
 + (nullable instancetype)modelWithDictionary:(NSDictionary *)dictionary;
 // json转模型
 + (nullable instancetype)modelWithJSON:(id)json;
 // 模型转NSObject
 - (nullable id)modelToJSONObject;
 // 模型转NSData
 - (nullable NSData *)modelToJSONData;
 // 模型转json字符串
 - (nullable NSString *)modelToJSONString;
 // 模型深拷贝
 - (nullable id)modelCopy;
 // 判断模型是否相等
 - (BOOL)modelIsEqual:(id)model;
 // 属性数据映射,用来定义多样化数据时转换声明
 + (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper;
 // 属性自定义类映射,用来实现自定义类的转换声明
 + (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass;
 // 属性黑名单,该名单属性不转换为model
 + (nullable NSArray<NSString *> *)modelPropertyBlacklist;
 // 属性白名单,只有该名单的属性转换为model
 + (nullable NSArray<NSString *> *)modelPropertyWhitelist;
 // JSON 转为 Model 完成后,该方法会被调用,返回false该model会被忽略
 // 同时可以在该model中做一些,转换不能实现的操作,如NSDate类型转换
 - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic;
 // Model 转为 JSON 完成后,该方法会被调用,返回false该model会被忽略
 // 同时可以在该model中做一些,转换不能实现的操作,如NSDate类型转换
 - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic

 

posted on 2022-05-12 17:58  HJiang  阅读(532)  评论(0编辑  收藏  举报