0

我有一个登录屏幕,我将用户名和密码发布到登录页面。

如果登录用户详细信息正确,网络服务会给我 2 个响应,我会从请求中得到回复。

{“价值”:1}

如果用户详细信息有误,我会从请求中回复。

{“价值”:0}

我已经能够解析那个 JSON Result 给我一个日志输出

值:1 或值: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) {


}
}
4

1 回答 1

1

如果来自服务器的响应真的很简单{"value":1},那么您正确地将 JSON 解析为使用NSJSONSerialization.

但是在valuekey 下,有一个NSNumber, not的实例NSArray

您要检索value和检查的代码应如下所示:

NSNumber *defineJsonData = [json objectForKey:@"value"];

NSLog(@"value: %@", defineJsonData);

if ([defineJsonData integerValue] == 0) {
    NSLog(@"Wrong credentials");
}
else {
    NSLog(@"Welcome :-)");
}
于 2013-05-21T12:56:51.323 回答