我有这个失败的测试用例。任何人都知道如何解决它?
[TestMethod]
public void Should_deserialize_utc_datestring_to_utc_date_with_AlwaysUseUtc_true()
{
string dateString = "2014-06-02T21:00:00.0000000Z";
DateTime dateRaw = new DateTime(2014, 6, 2, 21, 0, 0, 0, DateTimeKind.Utc);
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
DateTime dateSerialized = JsonConvert.DeserializeObject<DateTime>(dateString, settings);
dateSerialized.Should().Be(dateRaw);
}
我得到的错误(使用ReSharper作为我的TestRunner)是:
测试方法When_json_serializing_dates.Should_deserialize_utc_datestring_to_utc_date_with_AlwaysUseUtc_true引发异常: Newtonsoft.Json.JsonReaderException:解析数字时遇到意外的字符:T。路径&#39;&#39;,第1行,第10位。
答案 0 :(得分:6)
问题是你缺少字符串周围的嵌入式引号(记住,它期待JSON)。如下更改dateString
将解决您的问题:
string dateString = "\"2014-06-02T21:00:00.0000000Z\"";