C# JSON 日期转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# JSON 日期转换相关的知识,希望对你有一定的参考价值。

public string ToJson<T>(T t)
{
     javascriptSerializer jss = new JavaScriptSerializer();
     var json = jss.Serialize(t);
     MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);   
     Regex reg = new Regex(@"\\/Date\((\d+)\)\\/");
     var newjson = reg.Replace(json,matchEvaluator);
    return newjson;
}


public string ConvertJsonDateToDateString(Match m)
{
      string result = string.Empty();
      DateTime dt = new DateTime();
      var timevalue = m.Groups[0].Value.Replace("\\/Date(“,"").Replace(")\\/","");
      long newtime = long.Parse(timevalue);
      dt =  dt.AddMilliseconds(newtime);
      result = dt.ToString("yyyy-MM-dd HH:mm:ss");
      return result;
}

  

以上是关于C# JSON 日期转换的主要内容,如果未能解决你的问题,请参考以下文章