日期:2014-05-16 浏览次数:20389 次
Newtonsoft.Json.Converters.IsoDateTimeConverter iso = new Newtonsoft.Json.Converters.IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-dd hh:mm:ss";
//lst 为一个集合,第二个参数保持Json良好的格式,第三个是控制时间格式的参数 string json1 = JsonConvert.SerializeObject(lst,Formatting.Indented,iso); //另外一段代码,来自网络 public static string ToJson(object obj,bool formatjson) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); IsoDateTimeConverter idtc = new IsoDateTimeConverter(); idtc.DateTimeFormat = "yyyy-MM-dd hh:mm:ss"; JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(idtc); JsonWriter jw = new JsonTextWriter(sw); if (formatjson) { jw.Formatting = Formatting.Indented; } serializer.Serialize(jw, obj); return sb.ToString(); }