序列化的儿童
我必须消耗一个REST API,其中内容在子列表中具有可变结构。
这是一个简化的表示A 小型汽车具有多个组件: - 窗口,引擎和门
{
"ProductCode": 1234,
"ProductName": "SmallCar",
"Components": [
{
"PartType": "Window",
"Operation": "Electric",
"Tinted": true,
},
{
"PartType": "Engine",
"Capacity": 2000,
"Cylinders": 6,
"Layout": "V6"
},
{
"PartType": "Door",
"DoorStyle": "Standard"
}
]
}
定义了我的Poco类如下: -
public class Product
{
public int ProductCode {get; set;}
public string ProductName {get; set;}
public List<Component> Components {get; set;}
}
public class Component
{
public string PartType {get; set;}
}
public class Window : Component
{
public string Operation {get; set;}
public bool Tinted {get; set;}
}
public class Engine : Component
{
public int Capacity {get; set;}
public int Cylinders {get; set;}
public string Layout {get; set;}
}
public class Door : Component
{
public string DoorStyle {get; set;}
}
在避难所期间,如何获得它来创建适当的组件对象(窗口,门,发动机)。使用var product = deserializeObject&lt; product&gt;(....)
我只是获得组件
parttype 设置的列表,但我需要根据parttype
的列表为每个类类型
I have to consume a Rest API where the content has variable structure in a child list ..
Here is a simplified representation that a Small Car has several components:- Window, Engine and Door
{
"ProductCode": 1234,
"ProductName": "SmallCar",
"Components": [
{
"PartType": "Window",
"Operation": "Electric",
"Tinted": true,
},
{
"PartType": "Engine",
"Capacity": 2000,
"Cylinders": 6,
"Layout": "V6"
},
{
"PartType": "Door",
"DoorStyle": "Standard"
}
]
}
My POCO classes are defined as below:-
public class Product
{
public int ProductCode {get; set;}
public string ProductName {get; set;}
public List<Component> Components {get; set;}
}
public class Component
{
public string PartType {get; set;}
}
public class Window : Component
{
public string Operation {get; set;}
public bool Tinted {get; set;}
}
public class Engine : Component
{
public int Capacity {get; set;}
public int Cylinders {get; set;}
public string Layout {get; set;}
}
public class Door : Component
{
public string DoorStyle {get; set;}
}
During the deserialization, how can I get it to create the appropriate component object (Window, Door, Engine). With var product = DeserializeObject<Product>(....)
I just get a list of Components
with PartType
set, but I need the list to be of each class type according to the PartType
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自定义JSON转换器如果您使用的是
system.text.json
,并且有一个示例,说明如何做you can use Custom Json Converter if you are using
system.text.json
and there is an example of how you can do it