0

这是我的模型:

public class ReportAllMediaDetailsParams
    {
        public int profileID  { get; set; }
        public int organisationID { get; set; }
        public DateTime startDate { get; set; }
        public DateTime endDate { get; set; }
}

这是我的反序列化器:

var serializer = new JavaScriptSerializer();
var reportParams = serializer.Deserialize<ReportAllMediaDetailsParams>(json);

来自 json 的日期是:

"{\"profileID\":\"41\",\"organisationID\":\"2252\",\"startDate\":\"01/01/1970\",\"endDate\":\"01/01/1970\"}"
4

1 回答 1

3
  • Don't use JavaScriptSerializer, use Json.Net instead.

  • Don't use locale specific formats such as MM/dd/yyyy or dd/MM/yyyy in JSON. For example, does 1/4/2013 represent the first day of April? Or the fourth day of January? There's no way to know.

  • Use ISO8601 format instead. It is culture-invariant, so there is no ambiguity. In ISO format, you have 2013-01-04, which is always yyyy-mm-dd so there is no ambiguity. A full datetime would be 2013-01-04T05:30:27.123 for example.

  • Coming from C#, use DateTime.ToString("o") to get this format - or just use Json.Net which automatically serializes DateTime and DateTimeOffset using the ISO format.

于 2013-04-25T14:39:27.013 回答