DateTimes反序列化错误:JsonConvert返回错误的日期 [英] DateTimes deserializing wrong: JsonConvert is returning wrong dates

查看:51
本文介绍了DateTimes反序列化错误:JsonConvert返回错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在代码中从Solr检索数据以获取事件列表.我得到的结果的格式如下:

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; }

}

所以我得到了一个结果列表,一个总数和一个facetfields列表.在这种情况下,结果列表是EventResults列表:

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)
    {

    }
}

这是我的代码...

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);

    .....
}

为了简单起见,我进行了搜索,该搜索将仅返回一个事件,因此responseData在Results中仅包含一个事件.当我在调试时查看原始的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":[]}]}

那里有很多东西,但是重要的是StartDate设置为2014-07-23T00:00:00Z或2014年7月23日.数据中有两次出现StartDate,但是它们都是相同的.为了进行仔细检查,我添加了以下代码行以使用子字符串拉出StartDate,并且可以肯定的是,var startdate = 2014-07-23T00:00:00Z

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

但是,在下一行,当我检查Results(JsonConvert返回的反序列化对象)时,Results.Results中的一个事件的起始日期错误:

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

这似乎一直在进行;在我执行的每次搜索中,我检查的所有结果的开始日期都比responseData中的StartDate早了一天.

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.

我似乎无法进入JsonConvert.DeserializeObject方法,因此我不确定如何调试此问题.这是哪里出了问题的第一步,我不知道问题出在哪里.反序列化错误的唯一原因是StartDate(可能还有EndDate,尽管我还没有检查).

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.

推荐答案

Microsoft Json序列化程序(以及JavaScript中生成的Json反序列化程序)会将值转换为UTC,即使您在设置多变的.我最近遇到了这个问题,它使我发疯,因为它仅在从服务器返回数据时发生,而不是在从JavaScript发送时发生.最后,我必须先将日期转换为字符串,然后再返回客户端,然后使用 momentjs 库进行转换回到日期和时间.那时一切正常.

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() } };

    ...
}

然后在您的JS中:

var startDate = moment(Results.Results[0].StartDateString)

这篇关于DateTimes反序列化错误:JsonConvert返回错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆