惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - 阿拉伯顶峰

网页兼容调试笔记 NSoup解析处理Html jeasyui控件事件和方法的使用方法 正则表达式一 jeasyui-datagrid使用笔记 javascript笔记 excel拼接函数 2014年最简单、快捷的美股Scottrade开户攻略 将ppt转换成PDF chrome开发配置(四)生成项目及配置库引用 chrome开发配置(三)安装开发工具 chrome开发配置(二)获取源代码 chrome开发配置(一)安装配置工具 ie6 js报错unterminated string constant wso2 data services返回json数据方法 关于请求添加HttpRequestHeader SqlServer数据库正在还原的解决办法 dwz ie10一直提示数据加载中 Windows Server 2008下IIS 7配置ASP+ACCESS环境
JavaScriptSerializer 序列号datatime时少了8小时
阿拉伯顶峰 · 2014-10-10 · via 博客园 - 阿拉伯顶峰

有人说主要的因素是在于JSON格式不直接支持日期和时间。

简单一点处理办法是ToLocalTime()一下:dt = dt.ToLocalTime();

参考http://blog.csdn.net/cncdns/article/details/6164389 写个jsonhelp。不直接调用JavaScriptSerializer 的序列号就能解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;   
using System.Runtime.Serialization.Json;   
using System.IO;   
using System.Text;   
using System.Text.RegularExpressions;   
/// <summary>  
/// JSON序列化和反序列化辅助类  
/// </summary>  
public class JsonHelper  
{  
     /// <summary>  
    /// JSON序列化  
    /// </summary>  
     public static string JsonSerializer<T>(T t)  
     {  
         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));  
         MemoryStream ms = new MemoryStream();  
        ser.WriteObject(ms, t);  
         string jsonString = Encoding.UTF8.GetString(ms.ToArray());  
         ms.Close();  
         //替换Json的Date字符串  
         string p = @"///Date/((/d+)/+/d+/)///"; /*////Date/((([/+/-]/d+)|(/d+))[/+/-]/d+/)////*/
         MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);  
          Regex reg = new Regex(p);  
         jsonString = reg.Replace(jsonString, matchEvaluator);  
         return jsonString;  
     }  
     /// <summary>  
     /// JSON反序列化  
     /// </summary>  
     public static T JsonDeserialize<T>(string jsonString)  
     {  
         //将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"//Date(1294499956278+0800)//"格式  
         string p = @"/d{4}-/d{2}-/d{2}/s/d{2}:/d{2}:/d{2}";  
        MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);  
        Regex reg = new Regex(p);  
         jsonString = reg.Replace(jsonString, matchEvaluator);  
         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));  
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));  
         T obj = (T)ser.ReadObject(ms);  
         return obj;  
     }  
    
     /// <summary>  
     /// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串  
     /// </summary>  
     private static string ConvertJsonDateToDateString(Match m)  
     {  
         string result = string.Empty;  
         DateTime dt = new DateTime(1970,1,1);  
         dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));  
         dt = dt.ToLocalTime();  
         result = dt.ToString("yyyy-MM-dd HH:mm:ss");  
        return result;  
     }  
     /// <summary>  
     /// 将时间字符串转为Json时间  
     /// </summary>  
     private static string ConvertDateStringToJsonDate(Match m)  
     {  
         string result = string.Empty;  
         DateTime dt = DateTime.Parse(m.Groups[0].Value);  
         dt = dt.ToUniversalTime();  
         TimeSpan ts = dt - DateTime.Parse("1970-01-01");  
         result = string.Format("///Date({0}+0800)///",ts.TotalMilliseconds);  
        return result;  
    }  
 }