在C#中将Date转换为JSON对象

在C#中将Date转换为JSON对象

问题描述:

我用静态方法创建了一个C#类,该方法将任何对象转换为JSON对象.我为此使用了JavaScriptSerializar.这是我的代码

I've created a C# class with a static method that convert's any object to a JSON object. I've used JavaScriptSerializar for this. Here is my code

public class JS
{
    public static string GetJSON(object obj)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string retJSON = js.Serialize(obj);
        return retJSON;
    }
}

我有另一个只有两个属性的类,Date& Remark.这是我的课程

I've another class that have only two property, Date & Remark. Here is my class

public class RemarkData
{
    public DateTime Date { set; get; }
    public string Remark { set; get; }
}

现在,我正在使用以下代码将RemarkData类的对象转换为JSON

Now, I'm converting a object of the RemarkData class into JSON using following code

JS.GetJSON(objRemarkData);

这是我得到的输出

{"Date":"/Date(1389403352042)/",备注":"Sme备注"}

{"Date":"/Date(1389403352042)/","Remark":"Sme Remarks"}

这是我需要的输出

{日期":1389403352042,备注":一些备注"}

{"Date":1389403352042,"Remark":"Some Remarks"}

我应该怎么做才能得到这种输出?有帮助吗?

What should I do tho get that kind of output? Any help ?

double ticks = Math.Floor(objRemarkData.Date.ToUniversalTime() 
        .Subtract(new DateTime(1970, 1, 1))      
        .TotalMilliseconds); 
var newob = new { Date =ticks, Remark = objRemarkData.Remark};
JS.GetJSON(newob);