zoukankan      html  css  js  c++  java
  • 如何处理json字符转换为字典

    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?userName=%@&userPwd=%@",BASE_API,Login,_phoneNumTextField.text,_verificationCodeTextFiled.text]];
            
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            [manager POST:URL.absoluteString parameters:nil constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
               
                
                if(IsDictionary(responseObject)){
                    
                    NSString *str = [responseObject objectForKey:@"status"];
                    
                    NSDictionary *dic =[self dictionaryWithJsonString:[responseObject objectForKey:@"data"]];
                    
                    if ([str isEqualToString:@"0"]) {
                        
                        [Util saveResposeDefaults:dic];
                        
                        MainViewController *mMainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
                        
                        UINavigationController *mUINavigationController = [[UINavigationController alloc] initWithRootViewController:mMainViewController];
                        
                        [self presentViewController:mUINavigationController animated:YES completion:nil];
                        mUINavigationController = nil;
                    }else if ([str isEqualToString:@"2"]){
                        
                        NSLog(@"密码错误");
                        
                    }else{
                        
                        NSLog(@"用户不存在");
                    }
                    
                }
    
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                NSLog(@"Error: %@", error);
    
            }];
    -(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
        
        if (jsonString == nil) {
            
            return nil;
            
        }
        
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        
        NSError *err;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                             
                                                            options:NSJSONReadingMutableContainers
                             
                                                              error:&err];
        
        if(err) {
            
            NSLog(@"json解析失败:%@",err);
            
            return nil;
            
        }
        
        return dic;
        
    }

      

  • 相关阅读:
    经典8锁问题--助你彻底搞懂锁的概念
    linux上安装mysql
    Jenkins安装详解
    第一篇:实时网络日志分析器和交互式查看器--GoAccess安装
    Centos7上安装python3.7
    Nginx报错收集
    免费yum源镜像地址
    nginx日志文件切割
    腾讯云绑定和配置弹性网卡和添加弹性网卡
    LNMP-WEB应用环境搭建
  • 原文地址:https://www.cnblogs.com/wq-gril/p/5112615.html
Copyright © 2011-2022 走看看