NSInvalidArgumentException, reason: 'Invalid type in JSON write (__NSDate)'
当我尝试对nsdate对象进行json编码时,我得到了这个异常,我相信nsdate与json编码不兼容。但我必须对日期进行编码。有什么解决办法吗?
1 | *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSDate)' |
你在商店nsstring第一日期。然后你nsdate to convert字符串。P></
我知道你可以参考:P></
nsstring(与to nsdate转换回来)P></
nsstring to nsdate转换问题P></
nsstring nsdate how to convert to using nsdateformatter?P></
nsdate convert to nsstring尽量和编码。P></
1 2 3 4 5 6 7 8 9 10 11 | - (NSString *) changeDateToDateString :(NSDate *) date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; NSLocale *locale = [NSLocale currentLocale]; NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"hh mm" options:0 locale:locale]; [dateFormatter setDateFormat:dateFormat]; [dateFormatter setLocale:locale]; NSString *dateString = [dateFormatter stringFromDate:date]; return dateString; } |
你必须为你的第一noted,nsdate nsstring convert to an。它不是立即清楚,不管一个人多,which should be the公社中的日期格式。回答:the can be found here"how does not specify JSON本身通过JavaScript公社should be,but does"——iso8601。P></
here is an iso8601 from a method for helper类转换nsdate,埃里卡丧盾:礼貌P></
1 2 3 4 5 6 7 8 9 10 | - (NSString *)ISO8601 { struct tm time; time_t interval = [self timeIntervalSince1970]; gmtime_r(&interval, &time); char *x = calloc(1, 21); strftime_l(x, 20,"%FT%TZ", &time, gmtlocale); NSString *string = [NSString stringWithUTF8String:x]; free(x); return string; } |
如果你得到一iso8601 JSON字符串和后台在payload convert it to an想nsdate method for this class,使用nsdate:P></
1 2 3 4 5 6 7 | + (NSDate *)dateFromISO8601:(NSString *)string { if(!string) return nil; if (![string isKindOfClass:[NSString class]]) return nil; struct tm time; strptime_l([string UTF8String],"%FT%TZ", &time, gmtlocale); return [NSDate dateWithTimeIntervalSince1970:timegm(&time)]; } |
你有想?P></
1 | updateDate = [NSNumber numberWithFloat:[[NSDate date] timeIntervalSince1970]]; |
在这里描述的程序办理:not as nsdate SDK的支持对象P></
跟随这些步:P></
1。转换日期格式:JSON theP></
1 2 3 | NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease]; [formatter setDateFormat:@"Z"]; NSString *updateDate = [NSString stringWithFormat:@"/Date(%.0f000%@)/", [[NSDate date] timeIntervalSince1970],[formatter stringFromDate:[NSDate date]]]; |
2。在与后embed some阵列数组的。P></
the Way to store和扩retrieve JSON对象在nsdate摇篮使用timeintervalsince1970 to the property of nsdate。P></
nstimeinterval is the returned(双)可以转换easily好漂亮的标准和对象:在nsdate back to usingP></
1 | NSDate dateWithTimeIntervalSince1970:<#(NSTimeInterval)#> |
为我们的家园,我们是用JSON对象to convert to an地幔和我们的目标nsdate with one of its is missing jsontransformer物业P></
1 | @property (nonatomic) NSDate *expiryDate; |
where:P></
1 2 3 4 5 6 7 8 9 10 11 12 13 | + (NSValueTransformer *)expiryDateJSONTransformer { return [MTLValueTransformer transformerUsingForwardBlock:^id(NSString *dateString, BOOL *success, NSError *__autoreleasing *error) { return [self.dateFormatter dateFromString:dateString]; } reverseBlock:^id(NSDate *date, BOOL *success, NSError *__autoreleasing *error) { return [self.dateFormatter stringFromDate:date]; }]; } + (NSDateFormatter *)dateFormatter { NSDateFormatter *df = [NSDateFormatter new]; df.dateFormat = @"yyyy-MM-dd"; return df; } |
You must convert to the日期字符串之前你试图编码它。there are examples到处知道它足够容易should be to findP></