1

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

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

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

JS.GetJSON(objRemarkData);

Here is the output I'm getting

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

Here is the output that I need

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

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

3
  • You will probably register your own DateTime converter. Does answers to this question helps you: stackoverflow.com/questions/1341719/…? Commented Jan 11, 2014 at 1:32
  • That example is converting date in dd/mm/yyyy or some regular format that I specify. But how the date is converted into 1389403352042 this type of value? Commented Jan 11, 2014 at 1:41
  • It should be DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond Commented Jan 11, 2014 at 1:46

3 Answers 3

2
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);
Sign up to request clarification or add additional context in comments.

1 Comment

How do you get the JSON date string "/Date(1389403352042)/" to a DateTime object though? Just string operations to the the numeric value, and go from there?
1

You could try JSON.NET, it serializes Date into ISO string.

public class JS
{
    public static string GetJSON(object obj)
    {
        string retJSON = JsonConvert.SerializeObject(obj);
        return retJSON;
    }
}

Actually, you can use it directly, don't need to wrap inside another function.

This is also how asp.net web api serializes date objects. For more information why ISO string is a good choice, check out this link http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

Comments

0

This long number is "milliseconds since epoch". We can convert this to normal javascript date by using the following snippet as explained in another so post Converting .NET DateTime to JSON

var d = new Date();
d.setTime(1245398693390);
document.write(d);

One can also use a nice library from http://blog.stevenlevithan.com/archives/date-time-format with the following snippet ..

var newDate = dateFormat(jsonDate, "dd/mm/yyyy h:MM TT");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.