Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 序列化列表<;对象>;_C#_Xml_Json - Fatal编程技术网

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