2013-11-29 2 views
-1

json 웹 서비스의 데이터를 내 응용 프로그램에 액세스하고 있습니다. 언제든지 json 데이터가 없을 때마다 응용 프로그램이 다운되며, nsjsonserialization을 사용하고 있습니다. 어쨌든 응답 자체에서 빈 배열을 찾아서 오류를 표시 할 수 있으므로 응용 프로그램이 다운되지 않습니다.index에있는 객체가 빈 배열을 넘어서기 때문에 응용 프로그램이 손상됩니다.

+1

JSON 구문 분석 방법을 게시 할 수 있습니까? – logixologist

+0

응답 데이터를 한 번 확인하십시오. 응답 데이터의 길이가 일정한 경우 NSJson 직렬화 만 적용합니다. –

답변

1

이 시도합니다. . . .

NSArray * dataArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

if (dataArray != nil) { 

    // perform parsing 
} 
0

예상대로 예외 ..는 JSon Value Or Not이 있는지 여부를 조건을보십시오 :이 같은

뭔가 :

UserDetails = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil]; 
NSLog(@"User Det %@", UserDetails); 

if (!UserDetails){ 
    NSLog(@"Doesn't exist"); 
}else{ 
    NSlog(@"%@",[UserDetails objectForKey:@"lastName"]); 
     ..... 
} 
1

비어 있는지 확인하려면 다음

if ([myMutableArray count] != 0) { ... } 

당신이 변수가 nil이 있는지 확인하려면 다음

if (myMutableArray) { ... } 

나 :

if (myMutableArray != nil) { ... } 
0

데이터가 nil인지 또는 오류가 발생했는지 또는 데이터가 len인지 확인하십시오. gth

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
     { 
      NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode]; 
       NSLog(@"responseCode: %d", responseCode); //check response code 

      if ([data length] > 0 && error == nil) 

       //data downloaded -> continue 
      NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 

      else if ([data length] == 0 && error == nil) 
       //empty reply 
      else if (error != nil && error.code == ERROR_CODE_TIMEOUT) 
       // time out 
      else if (error != nil) 
       //error 
     }];