Objective c 解析Json以处理并引导用户查看控制器。目标C

Objective c 解析Json以处理并引导用户查看控制器。目标C,objective-c,json,parsing,nsurlconnection,response,Objective C,Json,Parsing,Nsurlconnection,Response,我有一个登录屏幕,我将用户名和密码发布到登录页面 如果登录用户详细信息正确,webservice会给我2个响应,我会从请求中得到返回的响应 {“值”:1} 如果用户详细信息有误,我会从请求中得到回复 {“值”:0} 我已经能够解析这个JSON结果,为我提供一个 值:1或值:0 我正在努力处理解析的json,例如 //parse out the json data NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlDa

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

如果登录用户详细信息正确,webservice会给我2个响应,我会从请求中得到返回的响应

{“值”:1}

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

{“值”:0}

我已经能够解析这个JSON结果,为我提供一个

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


}
}

如果来自服务器的响应实际上只是
{“value”:1}
,那么您可以使用
NSJSONSerialization
将JSON正确解析到字典

但是在
value
键下,有一个
NSNumber
的实例,而不是
NSArray

要检索
值并进行检查的代码应如下所示:

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

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

if ([defineJsonData integerValue] == 0) {
    NSLog(@"Wrong credentials");
}
else {
    NSLog(@"Welcome :-)");
}

如果([[json objectForKey:@“value”]isEqualToNumber:[NSNumber numberWithInt:1]])不客气的话,我也可以使用这行代码。是的,您的代码具有相同的结果。我的代码比较标量值,您的代码比较作为对象的数字。这意味着没有必要初始化NSNumber对象(
[NSNumber numberWithInt:1]