简体   繁体   中英

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

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,

  • How to convert it into proper 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 . 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.

    The solution is not to do this. Let the framework serialize your object for you by simply doing 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. 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. 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. Thus this version has no built-on logic to serialize these types in .Net core.

    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 . The question JsonSerializerSettings and Asp.Net Core shows how settings can be customized in ASP.NET Core.

    Secondly, you could convert your DataSet to a DTO and return that instead. For instance, the following extension methods convert any DataSet to a Dictionary<string, List<Dictionary<string, object>>> , which is serializable by 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.)

As of 25/04/2018. I had exactly same problem. Download latest version of Newtonsoft. I upgraded to 11.0.2. It now works with ASP Core 2. Datasets get converted to JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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