反序列化json对象时的时间错误

时间:2015-03-05 06:25:19

标签: c# json wcf

我正在反序列化类列表" pushNotification"使用JavaScriptSerializer,但我没有在结果中正确获得时间值。但是它从服务器端正确返回。我正在使用web http调用来获取此类的列表。下面是我的代码我在早上6点在" itemValue.ScheduledTime"但是从服务开始它就像上午11点一样被退回。

在JSON中从服务器返回的日期时间字符串是" 1425535200000 + 0500"并在数据库中" 2015-03-05 11:00:00.000"

// Restful service URL
string url = "http://localhost:4567/ClientService.svc/GetPendingNotification";
string strResult = string.Empty;

// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);

// set method as post
webrequest.Method = "GET";

// set content type            
// declare & read response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

// set utf8 encoding
// read response stream from response object
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream());
// read string from stream data
strResult = loResponseStream.ReadToEnd();

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, List<pushNotification>>>(strResult);

List<pushNotification> Notifications = new List<pushNotification>();

foreach (var itemValue in dict.Values.First())
{
    Notifications.Add(new pushNotification { Message = itemValue.Message, toAndroid = itemValue.toAndroid , toiOS = itemValue.toiOS, ScheduledDate = Convert.ToDateTime(itemValue.ScheduledDate), ScheduledTime = Convert.ToDateTime(itemValue.ScheduledTime)});            
}

1 个答案:

答案 0 :(得分:2)

JavaScriptSerializer无法理解时区偏移。请改用DataContractJsonSerializerhttps://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.110%29.aspx)(您必须使用DataMember / DataContract属性标记您正在序列化/属性的对象。