2016-09-15 86 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