问题
I'm retrieving data from Solr in my code to get a list of Events. The results I get is formatted like so:
public class SearchResults<T> where T : Result
{
public SearchResults()
{
Results = new List<T>();
}
public IEnumerable<T> Results { get; set; }
public int Total { get; set; }
public IEnumerable<FacetField> FacetFields { get; set; }
}
so I get a list of results, a total count, and a list of facetfields. The list of results in this case is a list of EventResults:
public class EventResult : Result
{
public string Location { get; set; }
public string DisplayDate { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Time { get; set; }
public string ImageUrl { get; set; }
public string WebsiteUrl { get; set; }
public string WebsiteText { get; set; }
public string CustomUrl { get; set; }
public string CustomUrlText { get; set; }
public string Description { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public IEnumerable<string> TaxonomyTypes { get; set; }
public IEnumerable<string> TaxonomyTypesId { get; set; }
public IEnumerable<string> TaxonomyTopics { get; set; }
public IEnumerable<string> TaxonomyTopicsId { get; set; }
public IEnumerable<string> TaxonomyLocations { get; set; }
public IEnumerable<string> TaxonomyLocationsId { get; set; }
public override void Load(XElement data)
{
}
}
and this is my code...
using (StreamReader sr = new StreamReader(stream))
{
var responseData = sr.ReadToEnd();
// added for testing
var startdate = responseData.Substring((responseData.IndexOf("StartDate") + 12), ((responseData.IndexOf("EndDate")-3) - (responseData.IndexOf("StartDate") + 12)));
var Results = JsonConvert.DeserializeObject<SearchResults<EventResult>>(responseData);
.....
}
I put in a search that will return exactly one event for simplicity, so responseData contains only one event in Results. When I look at the raw responseData while debugging, it looks like this:
{"Results":[{"Location":"Online","DisplayDate":"Jul 23, 2014","StartDate":"2014-07-23T00:00:00Z","EndDate":"2014-07-25T00:00:00Z","Time":"","Speakers":"","ImageUrl":"","WebsiteUrl":"","WebsiteText":"","CustomUrl":"","CustomUrlText":"","Description":"","Latitude":"","Longitude":"","TaxonomyTypes":[],"TaxonomyTypesId":[],"TaxonomyTopics":[],"TaxonomyTopicsId":[],"TaxonomyLocations":[],"TaxonomyLocationsId":[],"Id":"768","Title":"MFin Online Chat - 12:00 p.m.","Source":{"doc":{"str":[{"@name":"id","#text":"event_768"},{"@name":"s_eventId","#text":"768"},{"@name":"s_contact"},{"@name":"t_contact"},{"@name":"s_description"},{"@name":"t_description"},{"@name":"s_dateDisplay","#text":"Jul 23, 2014"},{"@name":"s_location","#text":"Online"},{"@name":"t_location","#text":"Online"},{"@name":"s_name","#text":"MFin Online Chat - 12:00 p.m."},{"@name":"t_name","#text":"MFin Online Chat - 12:00 p.m."},{"@name":"s_openTo"},{"@name":"t_openTo"},{"@name":"s_sponsors"},{"@name":"t_sponsors"},{"@name":"s_time"},{"@name":"s_latitude"},{"@name":"s_longitude"},{"@name":"s_speakers"},{"@name":"t_speakers"},{"@name":"s_customUrlText"},{"@name":"s_customUrl"},{"@name":"s_imageUrl"},{"@name":"s_websiteText"},{"@name":"s_websiteUrl"},{"@name":"t_taxonomy_topics"},{"@name":"t_taxonomy_types"},{"@name":"t_taxonomy_locations"},{"@name":"s_type","#text":"Event"},{"@name":"s_folderId","#text":"101"}],"arr":{"@name":"text","str":[null,null,"Online","MFin Online Chat - 12:00 p.m.",null,null,null,null,null,null]},"date":[{"@name":"dt_startDate","#text":"2014-07-23T00:00:00Z"},{"@name":"dt_endDate","#text":"2014-07-25T00:00:00Z"}],"long":{"@name":"_version_","#text":"1482239673606602769"}}}}],"Total":1,"FacetFields":[{"Name":"mv_taxonomy_topics","Values":[]},{"Name":"mv_taxonomy_topicsId","Values":[]},{"Name":"mv_taxonomy_types","Values":[]},{"Name":"mv_taxonomy_typesId","Values":[]},{"Name":"mv_taxonomy_locations","Values":[]},{"Name":"mv_taxonomy_locationsId","Values":[]}]}
There's a lot there, but the important thing is that StartDate is set to 2014-07-23T00:00:00Z, or July 23 2014. There are two occurrences of StartDate in the data, but they are both the same. To double check, I added the line to pull out the StartDate using substring, and sure enough, var startdate = 2014-07-23T00:00:00Z
However, at the next line, when I check Results (the deserialized object returned by JsonConvert), the one event in Results.Results has the wrong start date:
Results.Results[0].StartDate = 7/22/2014 8:00:00 PM
It seems to be doing this consistently; in every search I've done, all the results I've checked have had a start date of one day earlier than the StartDate in the responseData.
I can't seem to step into the JsonConvert.DeserializeObject method, so I'm not sure how to debug this issue; it's this one step where things go wrong, and I have no idea what the problem is. The StartDate (and probably EndDate, though I haven't checked) is the only thing getting deserialized wrong.
回答1:
The Microsoft Json serializer (and resulting Json deserializer in JavaScript) will convert the values to UTC, even if you specify a time as UTC on the server when setting the variable. I had this problem recently and it about drove me crazy since it only happens when returning the data from the server, not when sending from JavaScript. In the end I had to convert the date to a string before returning to the client, then used the momentjs library to convert it back to a date and time. Everything worked perfectly then.
public class EventResult : Result
{
...
public DateTime StartDate { get; set; }
public string StartDateString { get {return StartDate.ToString() } };
...
}
Then in your JS:
var startDate = moment(Results.Results[0].StartDateString)
来源:https://stackoverflow.com/questions/26509140/datetimes-deserializing-wrong-jsonconvert-is-returning-wrong-dates