2015-01-17 54 views
-1

我想獲取我的用戶名和密碼,我從我的服務器獲取。我已經存儲在jsondata這是NSMutableArray。不過,我只通過-objectAtIndex:獲得一個數據。我想打電話給我的字典裏面的所有數據。我知道我必須使用NSIndexSet,但我不知道使用它的正確方法。我正在使用for循環。我無法訪問我的密鑰值來匹配用戶憑據。有人可以提出任何建議嗎?這是我的代碼:如何使用NSDictionary訪問來自JSON數據的所有索引集合

NSDictionary *dict = [jsondata objectAtIndex:0]; 

    if ([username.text isEqualToString:[dict valueForKey:@"username"]]&&[password.text isEqualToString:[dict valueForKey:@"password"]]) { 
     username.text=nil; 
     password.text=nil; 

     [self performSegueWithIdentifier:@"login" sender:nil]; 

    } 
    else { 
     UIAlertView *error=[[UIAlertView alloc]initWithTitle:@"Oooops" message:@"Login Credentials did`t Match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
     [error show]; 

    }   

回答

0
NSString *tusername = nil; 
NSString *tpassword = nil; 
BOOL isLogin = NO; 

//jsondata contains many dictionary, so iterate the loop and find the dictionary that 
//contains username and password 
for(id temp in jsondata) 
{ 
    //check whether the object is Dictionary or not 
    if([temp isKindOfClass:[NSDictionary class]]) 
     { 

      NSDictionary *dict = temp; 
      if([[dict allKeys] containsObject:@"username"]) 
      { 
       tusername = [dict valueForKey:@"username"]; 
      } 
      else if([[dict allKeys] containsObject:@"password"]) 
      { 
       tpassword = [dict valueForKey:@"password"]; 
      } 
      //Check the condition here 
      if(tusername != nil && tpassword != nil) 
      { 
      if ([username.text isEqualToString:tusername] && [password.text isEqualToString:tpassword]) 
      { 
       isLogin = YES; 
       username.text=nil; 
       password.text=nil; 
     [self performSegueWithIdentifier:@"login" sender:nil]; 
     break; 
      } 
      } 

    } 
} 

    if(!isLogin) 
    { 
     UIAlertView *error=[[UIAlertView alloc]initWithTitle:@"Oooops" message:@"Login Credentials did`t Match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
     [error show]; 

    } 
+0

我不有任何特定的類在我的數據庫就像臨時或任何我有奧菱的用戶名密碼和ID,以及郵寄和他們本身的目錄.. – moeen

+0

溫度是不是一類。它是id類型的對象。id類型是所有Objective-C對象的泛型類型。我使用它,因爲我不知道jsondata包含的是什麼。 –

+0

使用此我得到單值gof用戶名除此之外,我想所有的值來檢查用戶名是否匹配我的json數據{「id」:「44」,「用戶名」:「moeen」,「密碼」 :「ahmad」,「emailid」:「[email protected]」},像dat很多字符串 – moeen

0

您需要調用一個循環來訪問jsondata上的所有數據。撥打如下:

for (int i=0; i<[jsondata count]; i++) 
{ 
NSDictionary *dict = [jsondata objectAtIndex:i]; 
    if ([username.text isEqualToString:[NSString stringWithFormat: @"%@",[dict valueForKey:@"username"]]]&&[password.text isEqualToString:[NSString stringWithFormat: @"%@",[dict valueForKey:@"password"]]]) { 
     username.text=nil; 
     password.text=nil; 

     [self performSegueWithIdentifier:@"login" sender:nil]; 

} 

    else { 
    UIAlertView *error=[[UIAlertView alloc]initWithTitle:@"Alert Title" message:@"Your alert messege" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
    [error show]; 

} 
} 
相關問題