简体   繁体   English

反序列化 JSON 时,我收到 `System.InvalidCastException`

[英]When deserializing JSON I'm getting `System.InvalidCastException`

So, basically, I have this simple JSON serializer/deserializer application in C# where I save StringCollections into .txt files that have JSON inside them.所以,基本上,我在 C# 中有这个简单的 JSON 序列化器/反序列化器应用程序,我将 StringCollections 保存到其中包含 JSON 的.txt文件中。 So, basically, I can successfully save my JSON to a raw text file like this:所以,基本上,我可以成功地将我的 JSON 保存到这样的原始文本文件中:

String path = Properties.Settings.Default.database + "//data.txt";
StringCollection data = Properties.Settings.Default.data;

File.WriteAllText(path, JsonSerializer.Serialize(data));

But when desirializing the file to get the object using this code:但是当反序列化文件以使用以下代码获取对象时:

string rawJson = File.ReadAllText(path);
StringCollection data = JsonSerializer.Deserialize<StringCollection>(rawJson);

It gives me this exception:它给了我这个例外:

System.InvalidCastException : 'Unable to cast object of type 'System.Text.Json.JsonElement' to type 'System.String'.' System.InvalidCastException :“无法将“System.Text.Json.JsonElement”类型的对象转换为“System.String”类型。”

I searched for this one StackOverflow and found a couple of posts about it, but they really didn't solve my problem.我搜索了这个 StackOverflow 并找到了几篇关于它的帖子,但它们确实没有解决我的问题。 What am I doing wrong?我究竟做错了什么?

NOTE: I'm using System.Text.Json and not Newtonsoft.Json .注意:我使用的是System.Text.Json而不是Newtonsoft.Json

As Per Microsoft documentation the below is the supported types https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to :根据 Microsoft 文档,以下是支持的类型https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to

.NET primitives that map to JavaScript primitives, such as numeric types, strings, and Boolean.
User-defined Plain Old CLR Objects (POCOs).
One-dimensional and jagged arrays (ArrayName[][]).
Dictionary<string,TValue> where TValue is object, JsonElement, or a POCO.
Collections from the following namespaces.
System.Collections
System.Collections.Generic
System.Collections.Immutable

So StringCollection is in System.Collections.Specialized namespace.所以StringCollectionSystem.Collections.Specialized命名空间中。 So its not supported, you have 2 choices所以它不支持,你有 2 个选择

  1. Implement Custom Converter : https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to实现自定义转换器: https : //docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to
  2. Change Your return type to for ex: List<string>将您的返回类型更改为例如: List<string>

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

相关问题 为什么我为自己的类型获取System.InvalidCastException? - Why am I getting a System.InvalidCastException for my own types? 为什么我收到“System.InvalidCastException: Specified cast is not valid”? - Why am I getting "System.InvalidCastException: Specified cast is not valid"? System.InvalidCastException &quot;对象必须实现 IConvertible。&quot; 使用 BinaryFormatter 反序列化字典时 - System.InvalidCastException "Object must implement IConvertible." when deserializing a Dictionary using a BinaryFormatter 使用 exe 应用程序的内存流程序集实例反序列化文件时出现 System.InvalidCastException - System.InvalidCastException when deserializing file with a memorystream assembly instance of an exe app 在C#中获取System.InvalidCastException - Getting System.InvalidCastException in c# 使用COM接口时System.InvalidCastException - System.InvalidCastException when using a COM interface 使用datagridview时发生System.InvalidCastException - System.InvalidCastException when work with datagridview 我该如何解决system.invalidcastexception - how can i solve the system.invalidcastexception 将SQL Server ROWVERSION放入System.Data.Linq.Binary属性时,出现System.InvalidCastException - System.InvalidCastException when getting SQL Server ROWVERSION into System.Data.Linq.Binary property 服务器上的System.InvalidCastException - System.InvalidCastException On Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM