/// <summary>
/// 将对象转换成json字符串
/// </summary>
/// <param name="obj">对象</param>
/// <param name="dateFormat">对象中日期要转换的格式</param>
/// <returns></returns>
public static String Json(object obj, String dateFormat = "yyyy-MM-dd")
{
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
timeConverter.DateTimeFormat = dateFormat;
var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore,Converters={timeConverter}};
var strjson=JsonConvert.SerializeObject(obj,Newtonsoft.Json.Formatting.Indented,jSetting);
strjson = Regex.Replace(strjson, @"\\/Date\((\d+)\)\\/", match =>
{
DateTime dt = new DateTime(1970, 1, 1);
dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
dt = dt.ToLocalTime();
return dt.ToString(dateFormat);
});
return strjson;
}
参考:http://www.cnblogs.com/jams742003/archive/2009/12/25/1632080.html