如何使用 NSDictionary 从 JSON 数据访问所有索引集
how to access all index set from JSON data using NSDictionary
我正在尝试获取我从服务器获取的用户名和密码。我已经存储在 jsondata
中,即 NSMutableArray
。但是我通过 -objectAtIndex:
只获得了一个数据。我想在字典中调用我的所有数据。我知道我必须使用 NSIndexSet
但我不知道正确的使用方法。我正在使用 for
循环。我无法访问我的密钥值以匹配用户凭据。有人可以提出任何建议吗?这是我的代码:
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];
}
您需要调用一个循环来访问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];
}
}
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];
}
我正在尝试获取我从服务器获取的用户名和密码。我已经存储在 jsondata
中,即 NSMutableArray
。但是我通过 -objectAtIndex:
只获得了一个数据。我想在字典中调用我的所有数据。我知道我必须使用 NSIndexSet
但我不知道正确的使用方法。我正在使用 for
循环。我无法访问我的密钥值以匹配用户凭据。有人可以提出任何建议吗?这是我的代码:
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];
}
您需要调用一个循环来访问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];
}
}
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];
}