This project has moved. For the latest updates, please go here.

Datetime parse

Mar 23, 2009 at 2:47 PM

When i use the Serialize method on my object datetime the result StringBuilder is this:

{"Data":new Date(1237819082406)} but should be {"Data":"new Date(1237819082406)"}

Json parse return an error.

This is my code:

Public Class myClass
    <JsonProperty("Data")> Public Data As DateTime 
End Class

Public Shared Function ObjectToJson(ByVal myObject As Object) As StringBuilder
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim writer As JsonWriter = New JsonTextWriter(sw)
        Dim serializer As JsonSerializer = New JsonSerializer()
        serializer.Converters.Add(New Converters.JavaScriptDateTimeConverter)
        serializer.Serialize(writer, myClass) 
        ObjectToJson = sb
 End Function

thanks for your help.

Mar 23, 2009 at 9:46 PM
No the first is correct. JavaScriptDateTimeConverter formats the date as a JS Date constructor, not a string.
Mar 24, 2009 at 11:20 AM
Thanks for your response!

But if pass this string to the js function... 

function CallbackComplete(s, e) {            
var jsonObj = JSON.parse(e.result); 
var mydate = eval("(" + jsonObj.Data + ")"

... JSON.parse(e.result) raises an error!

I can resolve this behavior?



Mar 25, 2009 at 6:16 AM
The answer depends upon how JSON.parse works. You need to find a date format it likes.
Mar 25, 2009 at 8:43 AM
My problem is this!

I use the json2.js library... you know there is one that behaves correctly for my scenario?

Sorry for my questions ... but I am not very experienced!

Thanks for your help!

Mar 26, 2009 at 8:08 AM
Edited Mar 26, 2009 at 9:04 AM
I don't know, I have never used json2.js. Perhaps you could do some investigation and then post the answer for everyone else.
Apr 12, 2009 at 10:42 PM