質問

I have NSDictionary values that i want to convert it to Json string like:

{
    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;
}

And the way i am converting it to json is:

- (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];
    }
}

I dont know why it always fails to convert.?

役に立ちましたか?

解決

Objects passed into NSJSONSerialization can only contain NSDictionary, NSArray, NSString, NSNumber, or NSNull.

NSDate objects must be explicitly converted to NSStrings first using an NSDateFormatter.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top