代码如下:
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();
}
生成的日期格式为一字符串,这样便于客户端处理!