String date in javascript in valid .Net format

I have a client side JSON object that I want to get on the server side.
For this, I have hidden text in which I put a lowercase version of my object.

$("#<%=hidden.ClientID%>").val(JSON.stringify(obj));

Then, on the server side, I try to deserialize it using the JavaScriptSerializer.

My problem: a string object contains a date, and I cannot parse it using JavaScriptSerializer.
What I did: change the date format to fit the format. Net:

function FormatDate(date) {
    if (typeof (date.getTime) != "undefined") {
        return '\\/Date(' + date.getTime() + ')\\/'
    }

    return date;
}

It seems to give a nice format, but when I use JSON.stringify for an object with well-formatted dates, it adds an extra backslash, so JavaScriptSerializer still can't get it.

, ?

+3
3

'\/Date(' + date.getTime() + ')\/';

. .

+1

.

var data = JSON.stringify(object);
data = data.replace(/\\\\/g, "\\");
+1

An old question, but if someone arrived here, how I, looking for a solution, found this to work: fooobar.com/questions/404868 / ...

function customJSONstringify(obj) {
    return JSON.stringify(obj).replace(/\/Date/g, "\\\/Date").replace(/\)\//g, "\)\\\/")
}
+1
source

All Articles