• JsonString,字典,模型之间相互转换


    NSData转字符串
    [NSString alloc] initWithData: encoding:]
    
    
    模型转字典
    attInfo.keyValues
    
    
    字典转模型
    ZTEOutputInfo *outputInfo = [ZTEOutputInfo objectWithKeyValues:responseObject];
    
    
    模型转Json字符串
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:attInfo.keyValues
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil ];
    NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] ;
    
    
    综合例子:
    + (void)checkAttatchStatus:(ZTEATTInfo *)attInfo t:(NSString *)t
    success:(void (^)(NSMutableDictionary *response))success failure:(void (^)(NSError *error))failure{
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:attInfo.keyValues
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:nil ];
        NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] ;
        
        ZTEInputInfo *inputInfo = [[ZTEInputInfo alloc] init];
        inputInfo.C = @"SubmitMCL";
        inputInfo.D = jsonString;
        //    CCLog(@"----D:%@",attInfo.keyValues);
        ZTEFilterInfo *filterInfo = [[ZTEFilterInfo alloc] init];
        filterInfo.K = @"ID";
        filterInfo.V = t;
        inputInfo.F = @[filterInfo];
        
        NSString* serverip = [ZTEServerAddrMng checkMailStatusAddr2];
        
        [kAppDelegate.netManager  postPath:serverip parameters:inputInfo.keyValues
                      postDataEncodingType:PostDataEncodingTypeJSON
                                   success:^(EMMHTTPRequestOperation *operation, id responseObject) {
                                       CCLog(@"Success: %@", responseObject);
                                       
                                       ZTEOutputInfo *outputInfo = [ZTEOutputInfo objectWithKeyValues:responseObject];
                                       CCLog(@"-------%@",outputInfo.keyValues);
                                       
                                       if ([outputInfo.S integerValue ] == 1) {
                                           if (success) {
                                               NSMutableDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:[(NSString *)outputInfo.D dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                                               success(resultDict);
                                           }
                                       }else{
                                           [ZTETipInfoView showTipWithMessage:outputInfo.M andState:KaBuTipInfoState_Failed];
                                       }
                                       
                                   } failure:^(EMMHTTPRequestOperation *operation, NSError *error) {
                                       CCLog(@"Error: %@", error);
                                       [ZTETipInfoView showTipWithMessage:@"网络错误" andState:KaBuTipInfoState_Failed];
                                       if(failure){
                                           failure(error);
                                       }
                                   }];
        
    }
    
  • 相关阅读:
    ChineseAlphabetUtil获取汉字首字母工具类
    RandomCodeUtil随机数工具类,随机生成数字、字母、数字字母组合、中文姓名
    ValidateUtil常用验证工具类,如手机、密码、邮箱等
    聊天项目
    日期
    字符串
    java中属性,set get 以及如何学习类的一些用法
    继承 多态 封装
    方法 属性 构造方法和包
    面向对象知识
  • 原文地址:https://www.cnblogs.com/HJiang/p/5083497.html
Copyright © 2020-2023  润新知