码迷,mamicode.com
首页 > Web开发 > 详细

如何处理json字符转换为字典

时间:2016-01-08 11:38:04      阅读:1117      评论:0      收藏:0      [点我收藏+]

标签:

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;
    
}

 

  

如何处理json字符转换为字典

标签:

原文地址:http://www.cnblogs.com/wq-gril/p/5112615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!