2

我需要这种格式的 JSON 日期格式:docDate":"1360905419116"、","inDocDate":"1360905419116" 等。

我的序列化:

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string result = jsonSerializer.Serialize( new {
    docDate = DateTime.Now,
    inDocDate = DateTime.Now,
    outDocDate = DateTime.Now } );

序列化后我收到的结果:

{"docDate":"\/Date(1360905419116)\/","inDocDate":"\/Date(1360905419116)\/","outDocDate":"\/Date(1360905419116)\/"}

我希望有

{"docDate":"1360905419116","inDocDate":"1360905419116","outDocDate":"1360905419116"}
4

1 回答 1

0

您可以使用此扩展方法

public static class DateTimeExtensions
{
  internal static readonly long DatetimeMinTimeTicks = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;

  public static long ToJsTime(this DateTime dateTime)
  {
    return (dateTime.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 10000L;
  }
}

所以你的代码变成:

string result = jsonSerializer.Serialize( new {
    docDate = DateTime.Now.ToJsTime().ToString(),
    inDocDate = DateTime.Now.ToJsTime().ToString(),
    outDocDate = DateTime.Now.ToJsTime().ToString() } );
于 2013-02-16T14:35:54.507 回答