关于c#序列化为json对象的问题 用JavaScriptSerializer序列化实体。里面有个datetime类型的成员,最后json输出是 \/Date(-2209017600000)\/这样的日期格式怎么处理? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JSON处理 给你转贴一篇文章:Json 的日期格式与.Net DateTime类型的转换Json 的日期形式大概是这样:"/Date(1242357713797+0800)/" , 甭管它的格式是多么不友好(因为单从形式看根本不知道何年何月),如果这个Date只是一个属性的话, Json.Net 已经为我们处理好了。但有些很特殊的时候,需要单独把这个Date转换为.Net的DateTime格式,那么下面的代码就可以帮到你了。这个代码我已经找了很多次,终于被我发现了,免去重复造轮子的劳动。这里跟大家分享一下, 可以保留毫秒,完全与原来结果一致。 static void Main(string[] args) { string [] jsonDates = {"/Date(1242357713797+0800)/" , "/Date(1242357722890+0800)/"}; foreach (string jsonDate in jsonDates) { Console.WriteLine("Json : {0}", jsonDate); DateTime dtResult = JsonToDateTime(jsonDate); Console.WriteLine("DateTime: {0}", dtResult.ToString("yyyy-MM-dd hh:mm:ss ffffff")); } Console.Read(); } public static DateTime JsonToDateTime(string jsonDate) { string value = jsonDate.Substring(6, jsonDate.Length - 8); DateTimeKind kind = DateTimeKind.Utc; int index = value.IndexOf('+', 1); if (index == -1) index = value.IndexOf('-', 1); if (index != -1) { kind = DateTimeKind.Local; value = value.Substring(0, index); } long javaScriptTicks = long.Parse(value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture); long InitialJavaScriptDateTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks; DateTime utcDateTime = new DateTime((javaScriptTicks * 10000) + InitialJavaScriptDateTicks, DateTimeKind.Utc); DateTime dateTime; switch (kind) { case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(utcDateTime.ToLocalTime(), DateTimeKind.Unspecified); break; case DateTimeKind.Local: dateTime = utcDateTime.ToLocalTime(); break; default: dateTime = utcDateTime; break; } return dateTime ; }结果如下:原文地址:http://www.cnblogs.com/coolcode/archive/2009/05/22/1487254.html 谢谢看到文章上面的方法了var text = data.replace(new RegExp('(^|[^\\\\])\\"\\\\/Date\\((-?[0-9]+)\\)\\\\/\\"', 'g'), "$1new Date($2)"); return eval("("+ text +")"); 如何扫描C盘下所有doc文件,并打成压缩包呢? 请教各位高手发邮件问题! 如何让我创建的excel.application与双击打开的excel文件使用不同的进程 .net 怎么检测服务器安装那些COM组件 C# 用进程A启动进程B,怎么样才能结束A,而B不会被结束 .net 开发的excel 执行菜单里有自定义函数源码。 一个关于统计点击的问题,,急急,,,谢谢 初学者问题:关于COM与AxtiveX 求助!编码转换问题 新手求指导VS与SQL server的连接 Winform 怎么向一个活动的窗口传递信息 C# 涂抹效果
{
string [] jsonDates = {"/Date(1242357713797+0800)/" , "/Date(1242357722890+0800)/"};
foreach (string jsonDate in jsonDates)
{
Console.WriteLine("Json : {0}", jsonDate);
DateTime dtResult = JsonToDateTime(jsonDate);
Console.WriteLine("DateTime: {0}", dtResult.ToString("yyyy-MM-dd hh:mm:ss ffffff"));
}
Console.Read();
} public static DateTime JsonToDateTime(string jsonDate)
{
string value = jsonDate.Substring(6, jsonDate.Length - 8);
DateTimeKind kind = DateTimeKind.Utc;
int index = value.IndexOf('+', 1);
if (index == -1)
index = value.IndexOf('-', 1);
if (index != -1)
{
kind = DateTimeKind.Local;
value = value.Substring(0, index);
}
long javaScriptTicks = long.Parse(value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture);
long InitialJavaScriptDateTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;
DateTime utcDateTime = new DateTime((javaScriptTicks * 10000) + InitialJavaScriptDateTicks, DateTimeKind.Utc);
DateTime dateTime;
switch (kind)
{
case DateTimeKind.Unspecified:
dateTime = DateTime.SpecifyKind(utcDateTime.ToLocalTime(), DateTimeKind.Unspecified);
break;
case DateTimeKind.Local:
dateTime = utcDateTime.ToLocalTime();
break;
default:
dateTime = utcDateTime;
break;
}
return dateTime ;
}结果如下:原文地址:http://www.cnblogs.com/coolcode/archive/2009/05/22/1487254.html
看到文章上面的方法了var text = data.replace(new RegExp('(^|[^\\\\])\\"\\\\/Date\\((-?[0-9]+)\\)\\\\/\\"', 'g'), "$1new Date($2)");
return eval("("+ text +")");