C# 序列化列表<;对象>;
我有一个类型对象的泛型列表,我正在尝试序列化它,但是反序列化并不能得到好的结果。 以下是我试图做的:C# 序列化列表<;对象>;,c#,xml,json,C#,Xml,Json,我有一个类型对象的泛型列表,我正在尝试序列化它,但是反序列化并不能得到好的结果。 以下是我试图做的: List<object> sample_list = new List<object>(); sample_list.Add(new Sample() { type=0,message="I am the first"}); sample_list.Add(new Sample1() { type=1,message1 = "I am t
List<object> sample_list = new List<object>();
sample_list.Add(new Sample() { type=0,message="I am the first"});
sample_list.Add(new Sample1() { type=1,message1 = "I am the 2" });
sample_list.Add(new Sample2() { type=2,message2 = "I am the 3" });
string serial= JsonConvert.SerializeObject(sample_list);
List<object> list = JsonConvert.DeserializeObject<List<object>>(serial);
lstbox.ItemsSource = list;
foreach(var item in list)
{
if (item is Sample)
{
MessageBox.Show("Item is sample");
}
}
List sample_List=new List();
sample_list.Add(newsample(){type=0,message=“我是第一个”});
sample_list.Add(newsample1(){type=1,message1=“我是2”});
sample_list.Add(newsample2(){type=2,message2=“我是3”});
string serial=JsonConvert.SerializeObject(示例列表);
List List=JsonConvert.DeserializeObject(串行);
lstbox.ItemsSource=列表;
foreach(列表中的变量项)
{
如果(项目为样品)
{
MessageBox.Show(“项目是示例”);
}
}
但消息框从未显示。
应该怎么做才能使其正常工作?您正在将列表反序列化到
对象的列表中,为什么您希望CLR会将这些对象识别为样本
或样本1
?序列化JSON外观:
[{"type":0,"message":"I am the first"},{"type":1,"message1":"I am the 2"},{"type":2,"message2":"I am the 3"}]
[{"$type":"ConsolePusher.Sample, ConsolePusher","type":0,"message":"I am the first"},
{"$type":"ConsolePusher.Sample1, ConsolePusher","type":1,"message1":"I am the 2"},
{"$type":"ConsolePusher.Sample2, ConsolePusher","type":2,"message2":"I am the 3"}]
那么JSON.NET如何神奇地发现它们是Sample
对象呢?做一个简单的测试,注意list[0].GetType().FullName
是Newtonsoft.Json.Linq.JObject
,而不是Sample
如果要反序列化到示例的列表
,请编写:
var list = JsonConvert.DeserializeObject<List<Sample>>(serial);
然后序列化的JSON看起来:
[{"type":0,"message":"I am the first"},{"type":1,"message1":"I am the 2"},{"type":2,"message2":"I am the 3"}]
[{"$type":"ConsolePusher.Sample, ConsolePusher","type":0,"message":"I am the first"},
{"$type":"ConsolePusher.Sample1, ConsolePusher","type":1,"message1":"I am the 2"},
{"$type":"ConsolePusher.Sample2, ConsolePusher","type":2,"message2":"I am the 3"}]
因此,它将能够在反序列化时重新创建对象:
var list = JsonConvert.DeserializeObject<List<object>>(serial,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
var list=JsonConvert.DeserializeObject(串行,
新JsonSerializerSettings
{
TypeNameHandling=TypeNameHandling.Objects
});
您正在将列表反序列化为对象的列表
,为什么希望CLR将这些对象识别为示例
或示例1
?序列化JSON外观:
[{"type":0,"message":"I am the first"},{"type":1,"message1":"I am the 2"},{"type":2,"message2":"I am the 3"}]
[{"$type":"ConsolePusher.Sample, ConsolePusher","type":0,"message":"I am the first"},
{"$type":"ConsolePusher.Sample1, ConsolePusher","type":1,"message1":"I am the 2"},
{"$type":"ConsolePusher.Sample2, ConsolePusher","type":2,"message2":"I am the 3"}]
那么JSON.NET如何神奇地发现它们是Sample
对象呢?做一个简单的测试,注意list[0].GetType().FullName
是Newtonsoft.Json.Linq.JObject
,而不是Sample
如果要反序列化到示例的列表
,请编写:
var list = JsonConvert.DeserializeObject<List<Sample>>(serial);
然后序列化的JSON看起来:
[{"type":0,"message":"I am the first"},{"type":1,"message1":"I am the 2"},{"type":2,"message2":"I am the 3"}]
[{"$type":"ConsolePusher.Sample, ConsolePusher","type":0,"message":"I am the first"},
{"$type":"ConsolePusher.Sample1, ConsolePusher","type":1,"message1":"I am the 2"},
{"$type":"ConsolePusher.Sample2, ConsolePusher","type":2,"message2":"I am the 3"}]
因此,它将能够在反序列化时重新创建对象:
var list = JsonConvert.DeserializeObject<List<object>>(serial,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
var list=JsonConvert.DeserializeObject(串行,
新JsonSerializerSettings
{
TypeNameHandling=TypeNameHandling.Objects
});
这里的线索应该是json:
[{"type":0,"message":"I am the first"},
{"type":1,"message1":"I am the 2"},
{"type":2,"message2":"I am the 3"}]
您正在将其反序列化为列表
。因此:json或API调用中没有任何东西提示您需要一个示例。如果希望它存储类型名并在反序列化过程中使用,则需要启用该选项:
var settings = new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.Objects
};
string serial = JsonConvert.SerializeObject(sample_list, settings);
List<object> list =
JsonConvert.DeserializeObject<List<object>>(serial, settings);
这里的线索应该是json:
[{"type":0,"message":"I am the first"},
{"type":1,"message1":"I am the 2"},
{"type":2,"message2":"I am the 3"}]
您正在将其反序列化为列表
。因此:json或API调用中没有任何东西提示您需要一个示例。如果希望它存储类型名并在反序列化过程中使用,则需要启用该选项:
var settings = new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.Objects
};
string serial = JsonConvert.SerializeObject(sample_list, settings);
List<object> list =
JsonConvert.DeserializeObject<List<object>>(serial, settings);
你调试应用程序了吗?为什么要序列化然后反序列化?@adaam这是一个非常常见的事情,如果你想检查你是否以一种允许你正确反序列化的方式序列化了数据…你调试了应用程序了吗?为什么要序列化然后反序列化?@adaam这是一个非常常见的事情如果您想检查您是否以允许您正确反序列化数据的方式序列化了数据,请执行此操作……您的“修复”几乎肯定不会起作用,因为实例并非都是示例
;一个是Sample
,一个是Sample1
,一个是Sample2
。您的“修复”几乎肯定不会起作用,因为实例并不都是Sample
;一个是样本
,一个是样本1
,一个是样本2
。