pre { /*控制代码不换行*/ white-space: pre; word-wrap: normal; }

Dates and Json.NET

对象中有日期,同时涉及到日期属性序列化的时候往往会造成一些麻烦,解决办法采用第三方组件 NetJson,

 

public class LogEntry
{
  public string Details { get; set; }
  public DateTime LogDate { get; set; }
}

public void WriteJsonDates()
{
  LogEntry entry = new LogEntry
  {
    LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
    Details = "Application started."
  };

  // default as of Json.NET 4.5
  string isoJson = JsonConvert.SerializeObject(entry);
  // {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}

  JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
  {
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
  };
  string microsoftJson = JsonConvert.SerializeObject(entry, microsoftDateFormatSettings);
  // {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}

  string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
  // {"Details":"Application started.","LogDate":new Date(1234656000000)}
}

 

转自:http://james.newtonking.com/json/help/index.html?topic=html/DatesInJSON.htm

 

 

 

 

posted @ 2014-01-01 00:33  monkey's  阅读(177)  评论(0)    收藏  举报