2014-02-13 4 views
0

난 내가 JSON의 같은 문자열로 변환 할 NSDictionary에 값이 :수 없습니다

{ 
    body = "10-feb"; 
    comments =  (
    ); 
    complete = 1; 
    "created_at" = "2014-02-09T15:56:01Z"; 
    "due_at" = "2014-01-10 20:00:00 +0000"; 
    "due_on" = "2014-10-02"; 
    id = 439824; 
    "notification_sent" = 0; 
    "notify_user" = 0; 
    "organization_id" = 972; 
    participants =  (
    ); 
    reminders =  (
    ); 
    starred = 0; 
    "updated_at" = "2014-02-09T15:56:01Z"; 
    "user_id" = 11129; 
} 

그리고 내가 JSON으로 변환하고있는 방법입니다 :

- (NSString*) jsonStringWithPrettyPrint:(BOOL) prettyPrint str:(NSDictionary *)str { 
    NSError *error = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:str 
                 options:(NSJSONWritingOptions) (prettyPrint ? NSJSONWritingPrettyPrinted : 0) 
                 error:&error]; 

    if (! jsonData) { 
     NSLog(@"jsonStringWithPrettyPrint: error: %@", error.localizedDescription); 
     return @"{}"; 
    } else { 
     return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
    } 
} 

나는 왜 그것이 항상 변환에 실패하는지 모른다.? 단지 NSDictionary, NSArray, NSString, NSNumber, 또는 NSNull를 포함 할 수 있습니다 NSJSONSerialization에 전달

+3

어떤 오류가 발생하고 있습니까? – Sulthan

+0

'due_at'는'NSDate'입니까? – Wain

+0

예 그것의 NSDate..i 예외적 인 중단 점을 추가했습니다.하지만 그만 멈추지 만 콘솔에서 스택 추적을 얻으려고 시도했지만 아무것도 로그 및 오류가 남아 있지 않습니다. –

답변

5

객체.

NSDate 개체는 NSDateFormatter을 사용하여 먼저 NSStrings으로 명시 적으로 변환되어야합니다.

+0

날짜 문자열을'NSString stringWithFormat : @ "% @", date]'및 문제가없는 NSString으로 변환했습니다. –

+0

JSON 데이터를 만드는 데 문제가 발생했습니다. 그러나 누군가는 데이터를 처리해야합니다. 문서를보고 그들이 기대하는 바를 찾으십시오. BTW. [myDate description]을 훨씬 쉽게 호출 할 수 있습니다. – gnasher729