简体   繁体   English

带有数据集的 JsonConvert.serializeObject 返回意外结果

[英]JsonConvert.serializeObject with dataset returning unexpected result

I am trying to convert dataset to JSON in .net core 2.0 and am using the below code to do that我正在尝试在 .net core 2.0 中将数据集转换为 JSON 并使用以下代码来做到这一点

return Ok(JsonConvert.SerializeObject(dataset, Formatting.Indented))

and the output I am getting is我得到的输出是

"{\\n \\"DataSet.RemotingVersion\\": {\\n \\"Major\\": 2,\\n \\"Minor\\": 0,\\n \\"Build\\": -1,\\n \\"Revision\\": -1,\\n \\"MajorRevision\\": -1,\\n \\"MinorRevision\\": -1\\n },\\n \\"XmlSchema\\": \\"<?xml version=\\\\\\"1.0\\\\\\" encoding=\\\\\\"utf-16\\\\\\"?>\\\\n<xs:schema id=\\\\\\"dataset\\\\\\" xmlns=\\\\\\"\\\\\\" xmlns:xs=\\\\\\"http://www.w3.org/2001/XMLSchema\\\\\\" xmlns:msdata=\\\\\\"urn:schemas-microsoft-com:xml-msdata\\\\\\">\\\\n <xs:element name=\\\\\\"dataset\\\\\\" msdata:IsDataSet=\\\\\\"true\\\\\\" msdata:UseCurrentLocale=\\\\\\"true\\\\\\">\\\\n <xs:complexType>\\\\n <xs:choice minOccurs=\\\\\\"0\\\\\\" maxOccurs=\\\\\\"unbounded\\\\\\">\\\\n <xs:element name=\\\\\\"datatable\\\\\\">\\\\n <xs:complexType>\\\\n <xs:sequence>\\\\n <xs:element name=\\\\\\"cvbf\\\\\\" type=\\\\\\"xs:string\\\\\\" msdata:targetNamespace=\\\\\\"\\\\\\" minOccurs=\\\\\\"0\\\\\\" />\\\\n </xs:sequence>\\\\n </xs:complexType>\\\\n </xs:element>\\\\n </xs:choice>\\\\n </xs:complexType>\\\\n </xs:element>\\\\n</xs:schema>\\",\\n \\"XmlDiffGram\\": \\"<diffgr:diffgram xmlns:msdata=\\\\\\"urn:schemas-microsoft-com:xml-msdata\\\\\\" xmlns:diffgr=\\\\\\"urn:schemas-microsoft-com:xml-diffgram-v1\\\\\\" />\\"\\n}"

Am pretty new to C# development,我对 C# 开发很陌生,

  • How to convert it into proper JSON?如何将其转换为正确的 JSON?
  • and Why is it appearing like this?为什么会这样?

You have two problems here:你在这里有两个问题:

  1. You seem to be double-serializing your dataset in the manner described in JSON.NET Parser seems to be double serializing my objects .您似乎以JSON.NET Parser 中描述的方式双重序列化您的dataset似乎是双重序列化我的对象 First you manually serialize your returned object to a string, then call Ok(object) which also serializes the object passed in (ie the JSON string), resulting in a nested serialization.首先,您手动将返回的对象序列化为字符串,然后调用Ok(object) ,它也会序列化传入的对象(即 JSON 字符串),从而导致嵌套序列化。

    The solution is not to do this.解决方案是不要这样做。 Let the framework serialize your object for you by simply doing return Ok(dataset);让框架通过简单地执行return Ok(dataset);为您序列化您的对象return Ok(dataset);

  2. Update.更新。 Json.NET 11.0 Release 1 supports .NET Standard 2.0, so serialization for DataSet and DataTable are now possible in .Net core with this and later versions. Json.NET 11.0 第 1 版支持 .NET Standard 2.0,因此现在可以在 .Net 核心中使用此版本和更高版本对DataSetDataTable进行序列化。 You need to upgrade to this or a subsequent version.您需要升级到此版本或后续版本。

    Original answer.原答案。 Your second problem is that you are trying to serialize a DataSet with Json.NET in .Net core using Json.NET 10.0.3 or earlier -- and unfortunately doing so is not yet supported.你的第二个问题是,你正在试图序列化DataSet使用Json.NET在.net中的核心与Json.NET 10.0.3或更早版本-不幸的是这样做目前还不支持。 As explained in Issue #1409: serializes a DataSet to JSON on dotnet core 2.0 and Issue #1383: .Net core build doesn't include DataTableConverter , this release of Json.NET targets netstandard 1.3 while DataSet and DataTable were introduced in a netstandard 1.5.问题 #1409:在 dotnet core 2.0 上将 DataSet 序列化为 JSON问题 #1383:.Net core build 不包括 DataTableConverter 中所述,此版本的 Json.NET 以 netstandard 1.3 为目标,而DataSetDataTable是在 netstandard 1.5 中引入的. Thus this version has no built-on logic to serialize these types in .Net core.因此,此版本没有内置逻辑来序列化 .Net 核心中的这些类型。

    So, what are your options using this version?那么,使用此版本您有哪些选择? Firstly, Json.NET is licensed under the MIT License so you could copy its versions of DataSetConverter and DataTableConverter , include them in your project, and add them to JsonSerializerSettings.Converters .首先,Json.NET 是在MIT 许可授权的,因此您可以复制其版本的DataSetConverterDataTableConverter ,将它们包含在您的项目中,并将它们添加到JsonSerializerSettings.Converters The question JsonSerializerSettings and Asp.Net Core shows how settings can be customized in ASP.NET Core.问题JsonSerializerSettings 和 Asp.Net Core显示了如何在 ASP.NET Core 中自定义设置。

    Secondly, you could convert your DataSet to a DTO and return that instead.其次,您可以将您的DataSet转换为DTO并返回它。 For instance, the following extension methods convert any DataSet to a Dictionary<string, List<Dictionary<string, object>>> , which is serializable by Json.NET:例如,以下扩展方法将任何DataSet转换为Dictionary<string, List<Dictionary<string, object>>> ,它可由 Json.NET 序列化:

     public static class DataSetExtensions { public static List<Dictionary<string, object>> ToDictionaryList(this DataTable table) { if (table == null) return null; return table.Rows.Cast<DataRow>() .Select(r => table.Columns.Cast<DataColumn>().ToDictionary(c => c.ColumnName, c => r[c])) .ToList(); } public static Dictionary<string, List<Dictionary<string, object>>> ToDictionary(this DataSet set) { if (set == null) return null; return set.Tables.Cast<DataTable>() .ToDictionary(t => t.TableName, t => t.ToDictionaryList()); } }

    Even better, create a strongly-typed model to return.更好的是,创建一个强类型模型来返回。 (If you are also serializing to XML then this is the better solution since XmlSerializer does not support serialization of dictionaries.) (如果您还序列化为 XML,那么这是更好的解决方案,因为XmlSerializer不支持字典的序列化。)

As of 25/04/2018.截至 25/04/2018。 I had exactly same problem.我有完全相同的问题。 Download latest version of Newtonsoft.下载最新版本的 Newtonsoft。 I upgraded to 11.0.2.我升级到 11.0.2。 It now works with ASP Core 2. Datasets get converted to JSON.它现在适用于 ASP Core 2。数据集被转换为 JSON。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM