[英]How to serialize JSON string containing date and time property using DataContractJsonSerializer?
we are trying to serialize a JSON object [as a string] into a custom class.我们正在尝试将 JSON object [作为字符串] 序列化为自定义 class。 While we rather not use any third-party packages such as Newtonsoft.Json or Json.NET, we tried to utilize DataContractJsonSerializer .
虽然我们宁愿不使用任何第三方包,例如 Newtonsoft.Json 或 Json.NET,但我们尝试使用DataContractJsonSerializer 。 The JSON object contains a DateTime property that is provided in the "yyyy-MM-dd HH:mm:ss" and when it comes to serialization the expectation below is thrown.
JSON object 包含在“yyyy-MM-dd HH:mm:ss”中提供的 DateTime 属性,当涉及到序列化时,会引发以下期望。
There was an error deserializing the object of type DateTime content '2020-05-29 09:05:39' does not start with '\/Date(' and end with ')\/' as required for JSON
根据 JSON 的要求,反序列化 DateTime 类型的 object 内容“2020-05-29 09:05:39”不以“\/Date(”开头并以“)\/”结尾时出错
IMPORTANT: the issue will be solved with NewtonSoft package and adding JsonSerializerSettings to JsonConvert.DeserializeObject.重要提示:该问题将通过 NewtonSoft package 并将 JsonSerializerSettings 添加到 JsonConvert.DeserializeObject 来解决。 As the final product is is a COM Object, our final target dll must have no dependant DLLs.
由于最终产品是 COM Object,因此我们的最终目标 dll 必须没有依赖的 DLL。
You can find the technical details below:您可以在下面找到技术细节:
JSON object is: JSON object 是:
{
...,
"export_time": "2020-05-29 09:05:39",
"app_version": "1.1.0",
"allowed_mac_addresses": [
"XX-XX-XX-XX-XX-XX"
],
"signature": ""
}
Target class:目标 class:
[DataContract]
public class MainFractionatorConfigFile
{
[DataMember]
internal string[] allowed_mac_addresses;
[DataMember]
internal DateTime export_time;
[DataMember]
internal string app_version;
[DataMember]
internal string signature;
}
and the serializer method is:序列化方法是:
public static MainFractionatorConfigFile ReadMainFractionatorConfigFile(string json)
{
var deserializedUser = new MainFractionatorConfigFile();
var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
var ser = new DataContractJsonSerializer(deserializedUser.GetType());
deserializedUser = ser.ReadObject(ms) as MainFractionatorConfigFile;
ms.Close();
return deserializedUser;
}
Attempt 1: There are some other suggestions in the community such as this solution but they demand other packages to be referenced.尝试 1:社区中还有一些其他的建议,例如这个解决方案,但他们要求引用其他包。
Attempt 2 was trying to add DateTimeDataContractSurrogate to DataContractJsonSerializer but was unsuccessful too.尝试 2试图将 DateTimeDataContractSurrogate 添加到 DataContractJsonSerializer 但也没有成功。
To sum up we are looking for an alternative method for the code below:总而言之,我们正在为以下代码寻找替代方法:
var mainFObj = JsonConvert.DeserializeObject<MainFractionatorConfigFile>(myJson, new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
});
Any help or idea will be appreciated.任何帮助或想法将不胜感激。
In the case of having a constraint about adding third-party NuGet packages, why not trying to clone/download the package (ie Newtonsoft.JSON) from Github and add them directly to your project.在限制添加第三方 NuGet 包的情况下,为什么不尝试从 Github 克隆/下载 package(即 Newtonsoft.JSON)直接添加到您的项目中。 So that you won't have any third-party dll your deployment (bin folder).
这样您的部署(bin 文件夹)就不会有任何第三方 dll。
Please note that the latest versions of the Newtonsoft.JSON are implemented in .netcore and while your project is on .net framework 4, you have to download the compatible version.请注意,最新版本的 Newtonsoft.JSON 在 .netcore 中实现,当您的项目在 .net 框架 4 上时,您必须下载兼容版本。 You can use the tags to find the best version to download.
您可以使用标签找到要下载的最佳版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.