2014-12-03 20 views
-1

我有這個方法通過JSON陣列登錄,這一切都確定,但它不是在JSON狀態不進入,如果

if(json2 == 0) 

但NSLog的輸入告訴我,它的值是0,我怎麼能解決這個 ?該方法正在工作,但它沒有進入,如果。

-(void)loginAPICall 
{ 

    NSString *device_name = @"Iphone"; 
    NSString *device_modelname = @"5"; 
    NSString *gcm_id = @"1234567890"; 
    NSString *user = _user.text; 
    NSString *pass = _pass.text; 
    // SENDING A POST JSON 
    NSString *post = [NSString stringWithFormat:@"device_name=%@&device_modelname=%@&gcm_id=%@&username=%@&password=%@", device_name, device_modelname, gcm_id, user, pass]; 

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[[NSURL alloc] initWithString:@"http://192.168.1.110/project/api"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 


    NSURLResponse *requestResponse; 
    NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil]; 
    NSError *error; 
    NSMutableDictionary *getJsonData = [NSJSONSerialization 
             JSONObjectWithData:requestHandler 
             options:NSJSONReadingMutableContainers 
             error:&error]; 

    if(error) 
    { 
     NSLog(@"%@", [error localizedDescription]); 
    } 
    else { 
     NSArray *json = getJsonData[@"login"]; 



     for (NSDictionary *jsn in json) 
     { 
      NSLog(@"id: %@ ", jsn[@"id_user"]); 

     } 

     NSArray *json2 = getJsonData[@"status"]; 
     if(json2 == 0){ 
      NSLog(@"Cannot login"); 
     } 
     NSLog(@"value:%@",json2); 

    } 
} 
+0

你可以發佈你的json數據嗎? – 2014-12-03 12:51:37

+1

array == 0你在做什麼? – sanjeet 2014-12-03 12:51:50

+0

NSLog告訴你*完全是什麼* ??? – 2014-12-03 13:02:12

回答

1

您是說由getJsonData[@"status"];對象返回是一個數組,但你正在檢查是否有integer

NSArray *json2 = getJsonData[@"status"]; 
if(json2 == 0){ 
    NSLog(@"Cannot login"); 
} 

如果您需要檢查的狀態數組返回是否有值做這樣thisL:

NSArray *json2 = getJsonData[@"status"]; 
if([json2 count] == 0){ 
    NSLog(@"Cannot login"); 
} 

如果節點狀態的對象返回是一個整數,然後試試這個:

NSNumber *json2 = getJsonData[@"status"]; 
if([json2 integerValue] == 0){ 
    NSLog(@"Cannot login"); 
} 
+1

他應該也*不*要檢查'錯誤',而是'getJsonData'。 – Droppy 2014-12-03 13:01:17

相關問題