[英]How to Serialize a Custom Object to Json in C#
I have a class like below我有一个像下面这样的课程
public class CustomObject
{
public string param1;
public IEnumerable<ExampleClass1> param2;
public Dictionary<ExampleClass1, IEnumerable<ExampleClass2>> param3;
//Constructor of the class with values set.
}
public class ExampleClass1
{
public string example1String;
public int example1Int;
//Constructor of the class with values set.
}
public class ExampleClass2
{
public string example2String;
public int example2Int;
//Constructor of the class with values set.
}
Input :输入 :
CustomObject
param1 : "abc"
param2 : List<ExampleClass1>
{abc1,1}
{abc2,2}
param3 : Map<ExampleClass1, List<ExampleClass2>>
<{abc1,1},[{xyz1,2},{xyz2,2}]>
<{abc2,2},[{xyz3,3},{xyz4,4}]>
Output :输出 :
param1 : "abc"
param2 : {abc1,1}
{abc2,2}
param3 :<{abc1,1},[{xyz1,2},{xyz2,2}]>
Expected Output预期产出
param1 : "abc"
param2 : {abc1,1}
{abc2,2}
param3 :{abc1,1},[{xyz1,2},{xyz2,2}]
{abc2,2},[{xyz3,3},{xyz4,4}]
I want to serialize this class and used the below approach to serialize the object to json string.我想序列化这个类并使用以下方法将对象序列化为 json 字符串。
var serializedContent = JsonConvert.SerializeObject(CustomObject);
The output is correct for param1 and param2 , but param3 is not getting serialized properly , It is showing only one record. param1 和 param2 的输出是正确的,但 param3 没有正确序列化,它只显示一条记录。
How can i solve this ?我该如何解决这个问题? I saw examples where it is possible to serialize a Dictionary but , if it is a member of a Class how do i achieve the complete serialization of the class ?我看到了可以序列化字典的示例,但是,如果它是类的成员,我如何实现类的完整序列化?
Take a look at this specifically in CustomObject
:在CustomObject
具体看一下:
public Dictionary<ExampleClass1, IEnumerable<ExampleClass2>> param3;
You set up a map, with the key being a class.你设置了一张地图,关键是一个类。 How does a map work where the key is an object?在键是对象的情况下,映射如何工作? It doesn't.它没有。 You have to specify the "value" of that class.您必须指定该类的“值”。 A quick way to do this is to override ToString
.一个快速的方法是覆盖ToString
。
So try this:所以试试这个:
public class ExampleClass1
{
public string example1String;
public int example1Int;
public override string ToString()
{
return example1String;
}
}
Keep in mind that making ExampleClass1
a key, you must ensure no other ExampleClass1
exist with the same value, otherwise you will have duplicate keys in your JSON which makes it tough to analyze.请记住,将ExampleClass1
键,您必须确保不存在其他具有相同值的ExampleClass1
,否则您的 JSON 中将有重复的键,这使得分析变得困难。
The caveat: I have no idea how you would deserialize this back to an actual object as it won't be able to convert a string to ExampleClass1
.警告:我不知道如何将其反序列化回实际对象,因为它无法将字符串转换为ExampleClass1
。 You will probably have to do something goofy where you do a composite key of your data, then make a custom JSON converter to deserialize it.您可能不得不在对数据进行组合键的情况下做一些愚蠢的事情,然后制作一个自定义的 JSON 转换器来反序列化它。
My suggestion to you is not use a class as your map key when you want to serialize to/from JSON.我对您的建议是,当您想要序列化到 JSON 或从 JSON 序列化时,不要使用类作为映射键。
ETA: data I used for testing: ETA:我用于测试的数据:
var cobj = new CustomObject
{
param1 = "abc",
param2 = new ExampleClass1[]
{
new ExampleClass1 { example1String = "abc1", example1Int = 1 },
new ExampleClass1 { example1String = "abc2", example1Int = 2 }
},
param3 = new Dictionary<ExampleClass1, IEnumerable<ExampleClass2>>
{
{ new ExampleClass1{ example1String = "abc1", example1Int = 1 }, new ExampleClass2[] { new ExampleClass2 { example2String = "xyz1", example2Int = 2 }, new ExampleClass2 { example2String = "xyz2", example2Int = 3 } } },
{ new ExampleClass1{ example1String = "abc2", example1Int = 4 }, new ExampleClass2[] { new ExampleClass2 { example2String = "xyz3", example2Int = 5 }, new ExampleClass2 { example2String = "xyz4", example2Int = 6 } } }
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.