我已经集成了OCMapper,用于将json字符串转换为自定义模型对象。我无法反序列化日期对象。例如我有一个json喜欢
NSString* sampleOutput=@"{\"UserType\":\"Admin\",\"testdate\":\"/Date(1438545600000+0400)/\"}";
NSData* sampleDate= [sampleOutput dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *loginDictionary = [NSJSONSerialization JSONObjectWithData:sampleDate options:NSJSONReadingAllowFragments error:&responseError];
MyModel* loginResponse= [[ObjectMapper sharedInstance] objectFromSource:loginDictionary toInstanceOfClass:[MyModel class]];
我的Model类结构是
`@interface MyModel : NSObject
@property(nonatomic,strong) NSString* userType;
@property(nonatomic,strong) NSDate* testdate;
@end`
但是我为testdate对象获得了nil值。请为我提供最佳方式。
如果json是
NSString* sampleOutput=@"{\"UserType\":\"Admin\",\"testdate\":\"2015-08-05\"}";
我可以按InCodeMappingProvider
喜欢
InCodeMappingProvider *inCodeMappingProvider = [[InCodeMappingProvider alloc] init];
NSDateFormatter *dateOfBirthFormatter = [[NSDateFormatter alloc] init];
[dateOfBirthFormatter setDateFormat:@"yyyy-dd-MM"];
[inCodeMappingProvider setDateFormatter:dateOfBirthFormatter forPropertyKey:@"testdate" andClass:[MyModel class]];
[[ObjectMapper sharedInstance] setMappingProvider:inCodeMappingProvider];
但我需要转换特定的C#日期序列化格式NSString* sampleOutput=@"{\"UserType\":\"Admin\",\"testdate\":\"/Date(1438545600000+0400)/
答案 0 :(得分:0)
最后我找到了答案。
在ObjectMapper.m->中processDictionary:
// Convert NSString to NSDate if needed
if ([propertyTypeString isEqualToString:@"NSDate"])
{
if ([value isKindOfClass:[NSDate class]])
{
nestedObject = value;
}
else if ([value hasPrefix:@"/Date("]){
nestedObject = [self myCustomDateConversion:value];
}
else if ([value isKindOfClass:[NSString class]])
{
nestedObject = [self dateFromString:value forProperty:propertyName andClass:class];
}
}