c# - iOS:OCMapper: Deserialize date from json -


i have integrated ocmapper converting json string custom model object.i cannot able deserialize date object. example have 1 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]]; 

my model class structure

`@interface mymodel : nsobject @property(nonatomic,strong) nsstring* usertype; @property(nonatomic,strong) nsdate* testdate; @end` 

but got nil value testdate object. kindly provide me best way so.

if json

 nsstring* sampleoutput=@"{\"usertype\":\"admin\",\"testdate\":\"2015-08-05\"}"; 

i can able parse date incodemappingprovider

like

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

but need convert specific c# date serialize format nsstring* sampleoutput=@"{\"usertype\":\"admin\",\"testdate\":\"/date(1438545600000+0400)/

finally found answer.

in objectmapper.m-> processdictionary:

// convert nsstring 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];                         }                     } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -