[英]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.