When using jquery to communicate with WCF, the problem of passing the date object through JSON is encountered. it is observed that the JSON string returned by WCF to the browser is in the form of "\/date (1231231323 + 0800. The preceding number 1231231323 is self-contained in the GMT time zone.1970The number of milliseconds that have elapsed since midnight, January 1, January 1 by normal time (non-timeout.
So we can retrieve this number to get the date object.
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> var datestr = " \/date (1231231323 + 0800) \/ " ;< br> var date = New date (parseint (datastr. substring ( 6 , datastr. length - 2 );
Of course, to pass the client's date object to WCF through a JSON string, you must format the date object as a string in the form of "\/date (1231231323 + 0800) \/".
VaR Date = New Date ();
VaR Datestr = " \/Date ( " + Date. gettime () + " + 0800 )\/ "
In json2.js, JSON. stringify and JSON. parse are used to convert JS objects and JSON strings. However, the two methods are converted in the ISO strings format by default.
View json2.js and find the following:CodeSegment
Date. Prototype. tojson = Function (Key) {
Return This . Getutcfullyear () + ' - ' +
F ( This . Getutcmonth () + 1 ) + ' - ' +
F ( This . Getutcdate ()) + ' T ' +
F ( This . Getutchours ()) + ' : ' +
F ( This . Getutcminutes ()) + ' : ' +
F ( This . Getutcseconds ()) + ' Z ' ;
} ;
Therefore, if you only need a WCF communication, you can modify this method to return a string in the form of WCF, so that you can directly call JSON. stringify (data)
That's OK. You don't need to process the time object any more.
Date. Prototype. tojson = Function (Key) {
Return "\/Date (" + This. Gettime ()+ "+ 0800 )\/";
} ;
Now let's take a look at how to make JSON. the parse method automatically processes the JSON time string returned by the server. the second parameter of the parse method is a callback function that can be used to traverse the attributes of the JSON object returned by WCF. Therefore, we can parse the JSON string returned by WCF in this way.
JSON. parse (data, Function (Key, value) {
If ( Typeof Value = " String " && Value. indexof ( " \/Date ( " ) = 0 ) {< br> var S = value. substring ( 6 , value. length - 2 );
return New date (parseint (s);
}
Return Value;
} )
Here, the string that determines the value is time is not very strict. You can modify it by yourself.
However, it is annoying to pass this function in every call. Anyway, I only communicate with WCF, so I can modify json2.js.
Find the following code snippet
JSON. parse = Function (Text, reviver) {
//The parse method takes a text and an optional reviver function, and returns
//A JavaScript value if the text is a valid JSON text.
VaRJ;
}
Add the following code before the parse method.
JSON. parse = Function (Text, reviver) {
// Modify xxx
If (Reviver = Undefined) {
Reviver = Function (Key, value) {
If ( Typeof Value = " String " && Value. indexof ( " \/Date ( " ) = 0 ) {
VaR S = Value. substring ( 6 , Value. Length - 2 );
Return New Date (parseint (s ));
}
Return Value;
}
} // Modify xxx
// The parse method takes a text and an optional reviver function, and returns
// A JavaScript value if the text is a valid JSON text.
VaR J;
}
In this way, we do not need to input
Second ParameterThe reviver callback function is available.
Json2.js file http://files.cnblogs.com/xhan/json2Xhan.js I modified