#region ToJson
/// <summary>
/// 生成压缩的json 字符串
/// </summary>
/// <param name="obj">生成json的对象</param>
public static string ToJson(object obj)
{
return ToJson(obj, false);
}
/// <summary>
/// 生成JSON字符串
/// </summary>
/// <param name="obj">生成json的对象</param>
/// <param name="formatjson">是否格式化</param>
public static string ToJson(object obj, bool formatjson)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
Newtonsoft.Json.Converters.IsoDateTimeConverter idtc = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
idtc.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Converters.Add(idtc);
Newtonsoft.Json.JsonWriter jw = new Newtonsoft.Json.JsonTextWriter(sw);
if (formatjson)
{
jw.Formatting = Newtonsoft.Json.Formatting.Indented;
}
serializer.Serialize(jw, obj);
return sb.ToString();
}
#endregion