反序列化 json 对象时出错
Wrong time while Deserialize json object
我正在使用 JavaScriptSerializer
反序列化 class "pushNotification" 的列表,但我没有在结果中正确获取时间值。但是它从服务器端正确返回。我正在使用对 wcf 的 Web http 调用来获取此 class 的列表。下面是我的代码,我在 "itemValue.ScheduledTime" 中得到 6:00AM 但是从服务中它像上午 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)});
}
JavaScriptSerializer
不理解时区偏移。请改用 DataContractJsonSerializer
(https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.110%29.aspx)(您必须使用 DataMember
/DataContract
属性标记您是 serializing/properties 的对象)。
我正在使用 JavaScriptSerializer
反序列化 class "pushNotification" 的列表,但我没有在结果中正确获取时间值。但是它从服务器端正确返回。我正在使用对 wcf 的 Web http 调用来获取此 class 的列表。下面是我的代码,我在 "itemValue.ScheduledTime" 中得到 6:00AM 但是从服务中它像上午 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)});
}
JavaScriptSerializer
不理解时区偏移。请改用 DataContractJsonSerializer
(https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.110%29.aspx)(您必须使用 DataMember
/DataContract
属性标记您是 serializing/properties 的对象)。