c# - JavaScriptSerializer won't recognize date string as DateTime? -
this question has answer here:
here model:
public class reportallmediadetailsparams { public int profileid { get; set; } public int organisationid { get; set; } public datetime startdate { get; set; } public datetime enddate { get; set; } }
here deserializer:
var serializer = new javascriptserializer(); var reportparams = serializer.deserialize<reportallmediadetailsparams>(json);
the date coming json is:
"{\"profileid\":\"41\",\"organisationid\":\"2252\",\"startdate\":\"01/01/1970\",\"enddate\":\"01/01/1970\"}"
don't use
javascriptserializer
, use json.net instead.don't use locale specific formats such
mm/dd/yyyy
ordd/mm/yyyy
in json. example,1/4/2013
represent first day of april? or fourth day of january? there's no way know.use iso8601 format instead. culture-invariant, there no ambiguity. in iso format, have
2013-01-04
, alwaysyyyy-mm-dd
there no ambiguity. full datetime2013-01-04t05:30:27.123
example.coming c#, use
datetime.tostring("o")
format - or use json.net automatically serializesdatetime
,datetimeoffset
using iso format.