2015-01-17 50 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]; 

} 
}