Iphone NSInvalidArgumentException,原因:';JSON写入(u NSDate)和#x27;
当我尝试用JSON编码NSDate对象时,我遇到了这个异常。我认为NSDate与JSON编码不兼容。但我必须对日期进行编码,有什么解决办法吗Iphone NSInvalidArgumentException,原因:';JSON写入(u NSDate)和#x27;,iphone,ios,json,nsdate,Iphone,Ios,Json,Nsdate,当我尝试用JSON编码NSDate对象时,我遇到了这个异常。我认为NSDate与JSON编码不兼容。但我必须对日期进行编码,有什么解决办法吗 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSDate)' 首先将数据存储在NSString中。然后将字符串转换为NSDate 你可以这样参考: 将NSDate转换
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSDate)'
首先将数据存储在NSString中。然后将字符串转换为NSDate 你可以这样参考:
将NSDate转换为NSString并尝试编码
- (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;
}
必须先将日期转换为字符串,然后才能对其进行编码。到处都有足够的例子,所以应该很容易找到你试过了吗
updateDate = [NSNumber numberWithFloat:[[NSDate date] timeIntervalSince1970]];
如下所述:
遵循以下步骤:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:@"Z"];
NSString *updateDate = [NSString stringWithFormat:@"/Date(%.0f000%@)/", [[NSDate date] timeIntervalSince1970],[formatter stringFromDate:[NSDate date]]];
1。将日期转换为JSON格式:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:@"Z"];
NSString *updateDate = [NSString stringWithFormat:@"/Date(%.0f000%@)/", [[NSDate date] timeIntervalSince1970],[formatter stringFromDate:[NSDate date]]];
2。嵌入某个数组并发布该数组。如前所述,必须首先将NSDate转换为NSString。然而,目前还不清楚日期应该用哪种格式表示。可以找到答案:“JSON本身没有指定日期的表示方式,但Javascript指定”——ISO8601 以下是NSDate助手类别的ISO8601转换方法,敬请注意: 如果您在JSON负载中获得ISO8601字符串,并希望将其转换为NSDate,请对NSDate使用以下类方法:
+ (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)];
}
在JSON中存储和检索NSDate对象的最简单方法是使用NSDate的timeIntervalSince1970属性 返回的NSTimeInterval(double)非常标准,可以使用以下方法轻松转换回NSDate对象:
NSDate dateWithTimeIntervalSince1970:<#(NSTimeInterval)#>
nsdatedatewithtimeintervals自1970年起:
在我们的例子中,我们使用Mantle将一个对象转换为JSON,我们的一个属性为NSDate的对象缺少JSONTransformer
@property (nonatomic) NSDate *expiryDate;
其中:
+ (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;
}
我在我的身体里做着类似的事情
// handle dates
var _params = [String: Any]()
params.forEach { (key, value) in
if let date = value as? Date {
_params[key] = DateFormatter.iso8601Full.string(from: date)
} else {
_params[key] = value
}
}
return try? JSONSerialization.data(withJSONObject: _params)
你到底为什么要用浮子?float有24位精度。自1970年以来已经超过10亿秒了。这意味着你的决心比一分钟还差。你的日期格式完全不标准。这没有意义。OP询问如何对NSDate进行编码。这意味着他们应该先将NSDate转换为NSString,然后再将其插入到试图进行JSON编码的字典中。我下面的答案显示了一个示例解决方案。这不是一个好的解决方案。这只是一个旁路。我需要知道如何使它与NSDate本身一起工作,或者,如果没有,为什么会发生这种情况。