2016-09-15 83 views
0

我想执行赛格瑞如果登录响应是与响应JSON验证登录

登录响应:

{ “登录”:[{ “参数userid”: “12”, “名称”:” (abc)“}]}

我该如何验证它?

下面是我提取响应的代码。

-(IBAction)loginbutton:(id)sender { 
    NSString *post = [[NSString alloc] initWithFormat:@"email==%@&pass==%@",self->email.text,self->passwrd.text]; 
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSURL *url = [NSURL URLWithString:@"URL"]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody:postData]; 
    NSURLResponse *response; 
    NSError *error; 
    NSData *urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; 
    NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; NSLog(@"Login response: is %@",str); //getting response 
} 
+0

解析JSON响应,并期待在您需要验证的任何值。 Objective-C中有无数解析JSON的例子。 – rmaddy

+0

@rmaddy我已经提到了我在问题中的回答,我想验证该问题以继续登录。 –

+0

没错。我告诉你你必须做什么。您需要解析JSON并从中获取所需的值。尝试使用您可以找到的无数现有示例来解析JSON。然后根据需要更新您的问题,一旦你到达那里。 – rmaddy

回答

0
NSString *post = [[NSString alloc] initWithFormat:@"email==%@&pass==%@",self->email.text,self->passwrd.text]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSURL *url = [NSURL URLWithString:@"URL"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody:postData]; 
NSURLResponse *response; 
NSError *error; 
NSData *urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; 
NSDictionary *jsondictionary = [NSJSONSerialization JSONObjectWithData:urlData options: NSJSONReadingMutableContainers error: &error]; 
NSLog(@"%@", jsondictionary); 

输出将是:

{ 
    Login: [{ 
    userid: 12, 
    name: (abc) 
    }] 
    } 

要获得用户的详细信息,请使用如下代码:

NSArray *userdetail = [jsondictionary objectForKey:@"Login"]; 
NSString *userID = [[userdetail firstObject]objectForKey:@"userid"]; 
NSLog(@"%@",userID);//12 
NSString *userName = [[userdetail firstObject]objectForKey:@"name"]; 
NSLog(@"%@",userName);//(abc) 
+0

value name is null登录=( { name =“(null)”; userid = 17; } ); } –

+0

用户创建期间,您没有发送用户名。服务器端问题。如果你想验证[[userdetail firstObject] objectForKey:@“name”]! =无|| [[userdetail firstObject] objectForKey:@“name”]!= null – Vinodh