JIL .NET JSON 反序列化异常

问题描述 投票:0回答:2

我使用以下命令将简单的反序列化为自己的类型事件:

JSON.Deserialize<Event>(text);

例外:

An exception of type 'Jil.DeserializationException' occurred in Jil.dll but was not handled in user code.
Additional information: Expected character: '\'

Newtonsoft 的 JSON 反序列化在相同的 json 上运行良好,JSONLint 也确认 JSON 是有效的。这里有什么线索吗?我尝试传入字符串,并按照 JIL 的 github 页面上的建议使用 using(StringReader) 。

.net json runtime-error deserialization jil
2个回答
8
投票

在没有看到 JSON 字符串的情况下,您尝试反序列化,我对此不确定,但最终反序列化器期望您尝试反序列化的日期(时间)采用另一种格式(我猜您正在尝试反序列化日期时间字段)。

JIL 似乎假设日期时间以“NewtosoftDateTime”形式交付,但您正在交付另一种格式。请参阅 https://github.com/kevin-montrose/Jil/blob/master/Jil/Deserialize/InlineDeserializer.cs#L667 了解 jil 如何假定您的日期格式的详细信息。

您可以通过选项更改预期的格式。在此处查看更多信息:https://github.com/kevin-montrose/Jil/blob/master/Jil/Options.cs


0
投票

.Net 中的 DateTime 有一种附加类型。它可以是 Local、Utc 或 Unspecified 三者之一。我相信 Jil 对于 ISO8601 格式应该有不同的行为。

目前在做:

JSON.Serialize(DateTime.Now, new Options(dateFormat: DateTimeFormat.ISO8601))

返回:

“2014-10-09T012:25:08.5660994”

© www.soinside.com 2019 - 2024. All rights reserved.