2014-05-20 5 views
0

다음 코드를 사용 중입니다. 가진ios 7에서 json 오류를 해결하는 방법은 무엇입니까?

2014-05-20 15:39:33.610 TMLP[2770:a0b] The internet is working via WIFI. 
2014-05-20 15:39:35.733 TMLP[2770:a0b] Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8e4a1a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x8e65ca0 "Request failed: not found (404)"} 
2014-05-20 15:39:35.734 TMLP[2770:a0b] -[NSError length]: unrecognized selector sent to instance 0x8e4a180 

2014-05-20 15:39:35.737 TMLP[2770:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSError length]: unrecognized selector sent to instance 0x8e4a180' 

* 먼저 던져 호출 스택 오류 다음

+(void)getQuarterList:(NSString *)user_id 
{ 
    if ([self checkInternet]) 
    { 
     NSString *url=[NSString stringWithFormat:@"%@/api/v1/quarters.json",MainURL]; 
     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
     NSDictionary *parameters = @{@"id":user_id}; 
     // NSDictionary *parameters = @{}; 

     // NSDictionary *parameters = @{@"id":user_id,@"auth_token":auth_token}; 
     [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *dict=[[NSDictionary alloc]initWithDictionary:responseObject]; 
       //NSMutableArray *dict=[[NSMutableArray alloc]initWithArray:responseObject]; 
      NSLog(@"dict%@",dict); 
      if ([dict valueForKey:@"Success"]) 
      { 
       NSNotification *notif1 = [NSNotification notificationWithName:@"quarterDetailsNotifier" object:[dict valueForKey:@"Success"]]; 
       [[NSNotificationCenter defaultCenter] postNotification:notif1]; 
      } 
      else if ([dict valueForKey:@"noData"]) 
      { 
       NSNotification *notif1 = [NSNotification notificationWithName:@"noDateNotifier" object:[dict valueForKey:@"Error"]]; 
       [[NSNotificationCenter defaultCenter] postNotification:notif1]; 
      } 

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Error: %@", error); 
      NSNotification *notif1 = [NSNotification notificationWithName:@"quarterDetailsFailNotifier" object:error]; 
      [[NSNotificationCenter defaultCenter] postNotification:notif1]; 
     }]; 
    } 
    else 
    { 
     NSNotification *notif1 = [NSNotification notificationWithName:@"internetFailNotifier" object:nil]; 
     [[NSNotificationCenter defaultCenter] postNotification:notif1]; 
    } 
} 

오전 : 이 오류

+0

당신이 그것을 말하는 일을 시도 했습니까? (JSON 텍스트는 배열 또는 객체로 시작되지 않고 조각이 ** 설정되지 않는 ** 옵션을 사용하여 시작되지 않았습니다.) [JSONSerializer] (http://cocoadocs.org/docsets/AFNetworking/2.2.4/Classes/AFJSONResponseSerializer.html#//api/name/serializerWithReadingOptions :) – Jkmn

+0

응답 할 수있는 앱의 콘텐츠 유형은 무엇입니까? 서버 코드가 콘텐츠에 대해 게시 한 헤더는 무엇입니까? 두 세트의 어떤 교차점이 있습니까? – holex

답변

0

를 해결하는 방법 당신은 NSNotification의 대상으로 error을 게시 할 수 있습니다. 그리고 notifcation 핸들러에서 NSString 또는 메소드 length을 가진 다른 객체로 사용합니다.

0

첫 번째 사항부터
획득 한 JSON 텍스트는 파서가 인식 할 수 있도록 항상 "["또는 "{"로 시작해야합니다. 그리고이 오류가 발생하는 이유는 이것이 성취되지 않았기 때문입니다. 온라인에서 사용할 수있는 JSON 검사기를 통해 JSON 텍스트를 확인하는 것이 좋습니다.

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error: &error]; 

options:NSJSONReadingAllowFragments이 문제를 해결할 수 있습니다
내가 제안 두 번째 것은 JSON 직렬화를 위해 사용하는 것입니다/역 직렬화는 다음과 같이 될 것이다

그리고 예 NSJSONSerialization

입니다 지금 파싱중인 파편을 파싱하는 것. 이 방법이 도움이되지 않으면 얻은 조각화 된 문자열에서 적절한 JSON 문자열을 추출하는 것이 좋습니다. 이는 문자열의 시작과 끝에서 여분의 원치 않는 문자를 제거하는 것을 의미합니다.

예제는 다음과 같습니다

NSURL *url=[NSURL URLWithString:@"yourURL"]; 
    NSData *response = [NSData dataWithContentsOfURL:url]; 
    NSString *badjsonData = [NSString stringWithUTF8String:[response bytes]]; 
    NSString *goodJsonData = [badjsonData substringFromIndex:76]; 
    NSString *finaljsonData = [goodjsonData substringToIndex:[goodjsonData length]-9]; 
    NSData *goodData = [finaljsonData dataUsingEncoding:NSUTF8StringEncoding]; 
    NSError *error; 
    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:goodData options:NSJSONReadingAllowFragments error: &error];