我有一个登录屏幕,我将用户名和密码发送到登录页面。
webservice给我2个响应,如果登录用户的详细信息是正确的,我从请求中得到一个返回的响应。
{"价值":1}
,如果用户的详细信息是错误的,我从请求返回。
{"价值":0}
我已经能够解析JSON结果给我一个日志输出
value: 1或value:0
我正在努力处理解析json,例如
//parse out the json data
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlData //1
options:kNilOptions
error:&error];
NSArray* defineJsonData = [json objectForKey:@"value"]; //2
NSLog(@"value: %@", defineJsonData); //3
if ([[json objectForKey:@"value"] isEqualToNumber:[NSNumber numberWithInt:1]])
{
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:0];
[self performSegueWithIdentifier: @"introScreenView" sender:self];
}
else {
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:0];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"Please try to login again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
下面是我剩下的代码。
- (void)myTask {
if ([userNameTextField.text isEqualToString:@""] || [passwordTextField.text isEqualToString:@""]) {
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:0];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Feilds Missing" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
NSString *data = [NSString stringWithFormat:@"UserName=%@&Password=%@",userNameTextField.text, passwordTextField.text];
NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
// preaparing URL request to send data.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSString *url = [NSString stringWithFormat:@"http://www.ddproam.co.za/Central/Account/LogOnIOS?"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[request setTimeoutInterval:7.0];
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Login response:%@",str);
NSHTTPCookie *cookie;
NSLog(@"name: '%@'n", [cookie name]);
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
NSLog(@"name: '%@'n", [cookie name]);
NSLog(@"value: '%@'n", [cookie value]);
NSLog(@"domain: '%@'n", [cookie domain]);
NSLog(@"path: '%@'n", [cookie path]);
}
//parse out the json data
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlData //1
options:kNilOptions
error:&error];
NSArray* defineJsonData = [json objectForKey:@"value"]; //2
NSLog(@"value: %@", defineJsonData); //3
if ([defineJsonData isEqual:0])
{
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:0];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"Please try to login again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else {
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:0];
[self performSegueWithIdentifier: @"introScreenView" sender:self];
}
if (theConnection) {
}
}
如果来自服务器的响应确实只是{"value":1}
,那么使用NSJSONSerialization
正确解析JSON到字典
但是在value
key下,有NSNumber
的实例,没有NSArray
的实例。
检索value
并检查它的代码应该是这样的:
NSNumber *defineJsonData = [json objectForKey:@"value"];
NSLog(@"value: %@", defineJsonData);
if ([defineJsonData integerValue] == 0) {
NSLog(@"Wrong credentials");
}
else {
NSLog(@"Welcome :-)");
}