使用反序列化后无法强制转换 json.net 字典中的对象
本文关键字:net json 字典 对象 转换 反序列化 | 更新日期: 2023-09-27 18:33:38
我在反序列化字典方面有问题。我无法将字典中的对象转换为我的类型 Remiza...我正在使用 Json.net,我可以看到字典中的对象是 JObject 而不是对象,我无法将它们转换为我的类型。这是我的序列化/反序列化代码:
private static Dictionary<Type, List<Object>> _ekstensje = new Dictionary<Type, List<Object>>();
public static void SerializeDictionary()
{
string json = JsonConvert.SerializeObject(_ekstensje);
System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:'tmp'dictionary.json");
file.WriteLine(json);
file.Close();
}
public static void DeserializeDictionary()
{
string json;
System.IO.StreamReader file = new System.IO.StreamReader(@"c:'tmp'dictionary.json");
json = file.ReadToEnd();
file.Close();
_ekstensje = JsonConvert.DeserializeObject<Dictionary<Type, List<Object>>>(json);//Deserializacja Dictionary
Debug.WriteLine(_ekstensje);
}
public static List<Object> GetEkstensja(Type className)
{
List<Object> list = _ekstensje[className];
return list;
}
执行:
ObjectPlus.DeserializeDictionary();
List<Object> list = ObjectPlus.GetEkstensja(typeof(Remiza));
foreach (Object o in list)
{
Remiza r = (Remiza) o;
listaRemiz.Add(r);
}
我的问题是,当投射到"Remiza"时,我有这个例外:
An exception of type 'System.InvalidCastException' occurred in Osek_MAS_WPF.exe but was not handled in user code. Additional information: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Osek_MAS_WPF.Remiza'.
感谢您的任何帮助!
这应该允许您将 JObect 转换为您的 Remiza 类型。
ObjectPlus.DeserializeDictionary();
List<Object> list = ObjectPlus.GetEkstensja(typeof(Remiza));
foreach (Object o in list)
{
Remiza r = o.ToObject<Remiza>();
listaRemiz.Add(r);
}
我从下面链接中的堆栈溢出答案中得到了这个。 如果我放的东西不起作用,请查看链接,它应该可以帮助您运行它。
https://stackoverflow.com/a/10221594/634769
为了使用 Json.NET 成功序列化和反序列化多态类型,您需要设置 TypeNameHandling = TypeNameHandling.Auto
,如下所示:
public class ObjectPlus
{
// Replace with whatever file name is appropriate. My computer doesn't have a "c:'tmp" directory.
static string JsonFileName { get { return Path.Combine(Path.GetTempPath(), "dictionary.json"); } }
static JsonSerializerSettings JsonSettings { get { return new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, Formatting = Formatting.Indented }; } }
private static Dictionary<Type, List<Object>> _ekstensje = new Dictionary<Type, List<Object>>();
public static void SerializeDictionary()
{
var path = JsonFileName;
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
using (var writer = new StreamWriter(stream))
{
var serializer = JsonSerializer.CreateDefault(JsonSettings);
serializer.Serialize(writer, _ekstensje);
}
}
public static void DeserializeDictionary()
{
var path = JsonFileName;
try
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
var serializer = JsonSerializer.CreateDefault(JsonSettings);
_ekstensje = serializer.Deserialize<Dictionary<Type, List<Object>>>(jsonReader);
}
}
catch (FileNotFoundException)
{
// File was not created yet, dictionary should be empty.
_ekstensje.Clear();
}
}
public static List<Object> GetEkstensja(Type className)
{
List<Object> list = _ekstensje[className];
return list;
}
public static void AddEkstensja<T>(T obj)
{
List<Object> list;
if (!_ekstensje.TryGetValue(obj.GetType(), out list))
list = _ekstensje[obj.GetType()] = new List<object>();
list.Add(obj);
}
internal static string ShowJsonContents()
{
if (!File.Exists(JsonFileName))
return string.Empty;
return File.ReadAllText(JsonFileName);
}
}
现在,当字典包含 Remiza
的实例时,您应该能够序列化和反序列化字典。
这将适用于序列化为对象或集合的类型。 但是,如果您的字典包含序列化为 JSON 基元的类型(例如 enum
或 long
),则可能需要将它们封装在类型包装器中,就像在 Json.Net 中将特定枚举反序列化为 system.enum 一样。
(顺便说一下,您的_ekstensje
字典不是线程安全的。