C#/ASP.NET JSON date deserialize

本文关键字:date deserialize JSON NET ASP | 更新日期: 2023-09-27 18:36:19

我正在尝试使用以下代码对对象进行去修饰,我想知道替换 json 日期的正确正则表达式是什么。 当我运行以下代码时,正则表达式永远不会被触发。 我在 json 字符串中使用标准的 JSON 日期格式。

{
    "UniqueId": "1000000003",     
    "Id": 3, 
    "ModifyTimestamp": "/Date(1338857699743)/"         
}
string json = // see above
string p = @"''/Date'(('d+)'+'d+')''/";
MatchEvaluator matchEvaluator = new MatchEvaluator(convertJsonDateToDateString);
Regex reg = new Regex(p);
json = reg.Replace(json, matchEvaluator);
JavaScriptSerializer serializer = new JavaScriptSerializer();            
Student student = serializer.Deserialize<Student>(json) as Student; 

public 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;
    }

C#/ASP.NET JSON date deserialize

这是一个完全有效的解决方案:

using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace ResolveOverlapIssue
{
    public partial class Form1 : Form
    {
        private static void DoSomethingElse<T>(ref T Id)
        {
            int i = 00;
        }
        public Form1()
        {
            InitializeComponent();
            string json = "{" +
                          "UniqueId: 1000000003," +
                          "Id: 3," +
                          "ModifyTimestamp: /Date(1338857699743)/" +
                          "}";
            MatchEvaluator matchEvaluator = ConvertJsonDateToDateString;
            var reg = new Regex(@".Date'('d+')'/.");
            json = reg.Replace(json, matchEvaluator);
        }
        public static string ConvertJsonDateToDateString(Match m)
        {
            var dt = new DateTime(1970, 1, 1);
            dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
            dt = dt.ToLocalTime();
            var result = dt.ToString("yyyy-MM-dd HH:mm:ss");
            return result;
        }
    }
}

@"''/Date部分看起来不对。它可能应该是@"'/Date...""''/Data...".(尾随"...''/"相同)