DateTimes反序列化错误:JsonConvert返回错误的日期

时间:2014-10-22 13:54:09

标签: json date datetime serialization

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

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列表:

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在结果中只包含一个事件。当我在调试时查看原始responseData时,它看起来像这样:

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

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

Results.Results[0].StartDate = 7/22/2014 8:00:00 PM

似乎一直这样做;在我完成的每次搜索中,我检查过的所有结果的开始日期都比responseData中的StartDate早一天。

我似乎无法进入JsonConvert.DeserializeObject方法,因此我不确定如何调试此问题;这是出错的一步,我不知道问题是什么。 StartDate(可能是EndDate,虽然我没有检查过)是唯一反序列化错误的东西。

1 个答案:

答案 0 :(得分:2)

即使您在设置变量时在服务器上指定UTC时间,Microsoft Json序列化程序(以及JavaScript中生成的Json反序列化程序)也会将值转换为UTC。我最近遇到了这个问题,它让我疯狂,因为它只发生在从服务器返回数据时,而不是从JavaScript发送时。最后,我必须在返回客户端之前将日期转换为字符串,然后使用momentjs库将其转换回日期和时间。一切都很完美。

public class EventResult : Result
{
    ...

    public DateTime StartDate { get; set; }

    public string StartDateString { get {return StartDate.ToString() } };

    ...
}

然后在你的JS中:

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