使用json.net 组件 生成JSON字符串,自定义日期格式
时间:2010-11-08 来源:疯狂秀才
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();
}
生成的日期格式为一字符串,这样便于客户端处理!
相关阅读 更多 +