[英]C# How do I correctly deserialize multiple custom objects in an array to an object?
I am retrieving a json object with an array, which contains multiple object types.我正在检索带有数组的 json 对象,该数组包含多种对象类型。
Given I retrieve a JSON payload that looks like this:鉴于我检索了一个如下所示的 JSON 有效负载:
{
"Lines": [
{
"PropertyA": "A",
"PropertyB": "B"
},
{
"Property01": 1,
"Property02": 2
}]
}
I want to deserialize this into a single object list.我想将其反序列化为单个对象列表。
Example:例子:
public List<Line> Lines;
So I can compare the object with one I expect.所以我可以将对象与我期望的对象进行比较。
What I have so far:到目前为止我所拥有的:
public class Class1
{
public string PropertyA = "A";
public string PropertyB = "B";
}
public class Class2
{
public int Property01 = 01;
public int Property02 = 02;
}
public class MainClass
{
public List<dynamic> Lines;
}
class Program
{
static void Main(string[] args)
{
string json = "{\r\n \"Lines\": [\r\n {\r\n \"PropertyA\": \"A\",\r\n \"PropertyB\": \"B\"\r\n },\r\n {\r\n \"Property01\": 1,\r\n \"Property02\": 2\r\n }]\r\n}";
MainClass actual = JsonConvert.DeserializeObject<MainClass>(json);
MainClass expected = new MainClass()
{
Lines = new List<dynamic>()
{
new Class1(),
new Class2()
}
};
actual.Should().BeEquivalentTo(expected);
}
}
Any help would be greatly appreciated!任何帮助将不胜感激!
Cheers干杯
Firstly change fields to properties.首先将字段更改为属性。 Then the easiest way here is to create one common object representing both objects like this:那么这里最简单的方法是创建一个表示两个对象的公共对象,如下所示:
public class Class1
{
public string PropertyA {get;set;}
public string PropertyB {get;set;}
public int Property01 {get;set;}
public int Property02 {get;set;}
}
public class MainClass
{
public List<Class1> Lines;
}
and deserialization would look like this:和反序列化看起来像这样:
MainClass actual = JsonConvert.DeserializeObject<MainClass>(json);
If you change class definition of Class1 to object with nullable properties you'll be able to detect if property existed in the json to differentiate between both objects:如果您将 Class1 的类定义更改为具有可为空属性的对象,您将能够检测 json 中是否存在属性以区分两个对象:
public class Class1
{
public string PropertyA {get;set;}
public string PropertyB {get;set;}
public int? Property01 {get;set;}
public int? Property02 {get;set;}
}
Another way is to use JArray and continue with your logic using this object:另一种方法是使用 JArray 并使用此对象继续您的逻辑:
JArray arr = JArray.Parse(json);
You can verify the propriety name and deserialize in right object like that您可以像这样验证专有名称并在正确的对象中反序列化
class Program
{
static void Main(string[] args)
{
var class1List = new List<Class1>();
var class2List = new List<Class2>();
var genericList = new List<dynamic>();
var actual = JsonConvert.DeserializeObject<dynamic>(json);
foreach (var item in actual.Lines)
{
string itemJson = JsonConvert.SerializeObject(item);
if (itemJson.Contains("PropertyA"))
{
var class1 = JsonConvert.DeserializeObject<Class1>(itemJson);
class1List.Add(class1);
genericList.Add(class1);
}
else
{
var class2 = JsonConvert.DeserializeObject<Class2>(itemJson);
class2List.Add(class2);
genericList.Add(class2);
}
}
}
}
public class Class1
{
public string PropertyA;
public string PropertyB;
}
public class Class2
{
public int Property01;
public int Property02;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.