简体   繁体   English

将json-String反序列化为DataSet之后的DateTime-System.FormatException

[英]DateTime-System.FormatException after deserialize json-String to DataSet

i have a big problem with a DateTime-Format after deserializing a json-string to a Dataset. 在将json字符串反序列化为数据集后,我对DateTime-Format有一个大问题。

Dim formatter = new JsonSerializerSettings() With { .DateFormatString = "yyyy-MM-ddThh:mm:ssZ" }
Dim json = JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented,formatter)

After this two lines I get a well formed DateTime-Format (eg "2015-11-24T09:36:00Z") 在这两行之后,我得到了格式正确的DateTime格式(例如“ 2015-11-24T09:36:00Z”)

But after that, I have to convert this json-string to a DataSet-Object. 但是之后,我必须将此json-string转换为DataSet-Object。 I do it like this: 我这样做是这样的:

Dim ds = JsonConvert.DeserializeObject(Of DataSet)(json,formatter)

And now, the DateTime-Value looks like this: "11/24/2015 09:36:00" 现在,DateTime-Value看起来像这样:“ 11/24/2015 09:36:00”

Finally this value ends in an Exception, that it is not a valide DateTime, if I want do parse it to an DateTime... 最后,此值以Exception结尾,如果我想将其解析为DateTime,则它不是有效的DateTime ...

DateTime.Parse("11/24/2015 09:36:00")

So maybe there are some formatters which i have to use? 因此,也许有一些我必须使用的格式化程序? I don't know... 我不知道...

Hope you can help me! 希望你能帮我! Thank you in advance! 先感谢您!

EDIT: Sample-Jsonstring: 编辑: Sample-Jsonstring:

{"records":[{"Key":"EIG*11111","ChangeDate":"2015-11-24T09:36:00Z"}]}

This ChangeDate will be formatted to #11/24/2015 09:36:00 AM# in the DataRow 此更改日期将在DataRow中格式化为#11/24/2015 09:36:00 AM#

JSON.NET has IsoDateTimeConverter class where you can specify date format to parse formatted UTC datetime in JSON string as DateTime with DateTimeFormat property. JSON.NET具有IsoDateTimeConverter类,您可以在其中指定日期格式,以使用DateTimeFormat属性将JSON字符串中格式化的UTC日期时间解析为DateTime The property usage should be like this: 属性用法应如下所示:

Dim formatter As New IsoDateTimeConverter With { .DateTimeFormat = "dd/MM/yyyy" }
Dim ds = JsonConvert.DeserializeObject(Of DataSet)(json, formatter)

Or use it in one-line form: 或以单行形式使用它:

Dim ds = JsonConvert.DeserializeObject(Of DataSet)(json, New IsoDateTimeConverter With { .DateTimeFormat = "dd/MM/yyyy" })

Note that you need adding Newtonsoft.Json.Converters namespace to enable IsoDateTimeConverter class. 请注意,您需要添加Newtonsoft.Json.Converters命名空间才能启用IsoDateTimeConverter类。

Afterwards, use DateTime.ParseExact() with specified format above to make sure that returned date string can be converted to DateTime type. 然后,使用上面指定格式的DateTime.ParseExact()来确保可以将返回的日期字符串转换为DateTime类型。

Similar issue (in C# context): 相似的问题(在C#上下文中):

Deserializing dates with dd/mm/yyyy format using Json.Net 使用Json.Net以dd / mm / yyyy格式反序列化日期

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将字符串识别为有效的DateTime(System.FormatException)? - String was not recognized as a valid DateTime(System.FormatException)? 无法将system.formatexception字符串识别为有效的日期时间 - system.formatexception string was not recognized as a valid datetime C#反序列化带有json-string的多个数组的字符串,显式列出<string> - C# Deserialize string of multiple arrays with json-string in it explicit to List<string> c# 中的 DateTime 解析:获取“System.FormatException:”String 未被识别为有效的 DateTime”错误 - DateTime parsing in c#:getting a 'System.FormatException: 'String was not recognized as a valid DateTime' error System.FormatException:字符串未被识别为有效的DateTime(DateTime格式yyyyMMdd:HHmmss) - System.FormatException: String was not recognized as a valid DateTime (DateTime Format yyyyMMdd:HHmmss) 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# 将字符串转换为DateTime时发生FormatException - FormatException while converting string to DateTime 字符串未被识别为有效的 DateTime FormatException - String was not recognized as a valid DateTime FormatException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM