简体   繁体   English

序列化不同类的对象列表(实现通用接口)仅呈现接口属性(作为 IActionResult)

[英]Serialize a list of Objects of different Classes (implementing a common Interface) rendering only the Interface Properties (as IActionResult)

I wrote a Controller Action that needs to return a Json with a List of Sports.我写了一个 Controller 动作,它需要返回一个带有运动列表的 Json。 This Json Array only needs to contain the common properties (defined at the Interface level).这个 Json 数组只需要包含公共属性(在接口级别定义)。

The Interface definition is:接口定义为:

public Interface ISport
{
    string TeamName {get; set;}
}

And the Implementation Classes are:实现类是:

public Class SoccerTeam : ISport
{
   string TeamName {get; set;}
   int YellowCardsPerGame {get; set;}
}
public Class BasketTeam : ISport
{
   string TeamName {get; set;}
   int ThreePointsPerGame {get; set;}
}

If I use the System.Text.Json JsonSerialier class with its Serialize method passing the Interface class (ISport) as a parameter, I get what I need :如果我使用 System.Text.Json JsonSerialier class 及其序列化方法传递接口 class (ISport) 作为参数,我得到我需要的:

IEnumerable<ISport> sportscollection = Repository.GetAllSports();    
string Json = JsonSerialier.Serialize<ISport>(sportscollection);

returns返回

[{"TeamName":"ChicagBuls"},{"TeamName":"Chelsea"}]

But I don't know how to cast this string into IActionResult in such a way that arrives to the browser as a Json object.但我不知道如何以 Json object 到达浏览器的方式将此字符串转换为 IActionResult。 If I try:如果我尝试:

return Ok(json);

or或者

return Json(json);

The browser gets a valid response, but the content is a string, not a Json structure.浏览器得到有效响应,但内容是字符串,而不是 Json 结构。

If I use the NetCore Controller Json method, the IActionResult gets the Json object I'm looking for, but the content of this object includes all the Class Properties, the ones from the Interface and the ones specific to the class. If I use the NetCore Controller Json method, the IActionResult gets the Json object I'm looking for, but the content of this object includes all the Class Properties, the ones from the Interface and the ones specific to the class. That is, if I make the call:也就是说,如果我打电话:

IActionResult Json = Json(sportscollection);

I get:我得到:

[{"TeamName":"ChicagBuls","ThreePointsPerGame ":25},{"TeamName":"Chelsea","YellowCardsPerGame ":3}]

Is there any way I can tell Controller Json method to use ISport type when Serializing?有什么方法可以告诉 Controller Json 方法在序列化时使用 ISport 类型? Is there a easy and clean way to use the Json String right information into a IActionResult?是否有一种简单而干净的方法可以将 Json 字符串权限信息用于 IActionResult?

I got the answer.我得到了答案。

I just need to return the Json String using the Controller method Content and specifying the content type as "application/json":我只需要使用 Controller 方法返回 Json 字符串并将内容类型指定为“application/json”:

return Content(sportscollection, "application/json");

first, if the Type of sportCollection was an array of Isport not children of it, after using json(sportCollection) you will see that you want so that you can use Cast<ISport>() right after getting them from the repository and send them.首先,如果 SportCollection 的TypesportCollection的数组Isport不是它的子项,则在使用json(sportCollection)之后,您将看到您想要的,以便您可以在从存储库获取它们并发送它们后立即使用Cast<ISport>() .
second, you can use JsonIgnore attribute top of each property of the (child) class to ignore them at serialization其次,您可以使用(子)class 的每个属性顶部的JsonIgnore属性在序列化时忽略它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ListBox,实现相同接口的不同对象的列表 - ListBox, list of different objects implementing the same interface 实现通用接口的类中的C#协方差 - C# Covariance in Classes Implementing Common Interface 在多个类内部实现结构,这些结构实现具有不同数据类型的公共接口 - Implementing structures inside multiple classes, which implement a common interface, with different data types 如何序列化/反序列化从公共类继承并遵循相同接口的对象列表? - How is it possible to serialize/deserialize a list of objects that inherit from a common class and adhere to the same Interface? 仅允许为特定类实现接口 - Allowing implementing interface only for specific classes 尝试序列化界面中的对象列表时出错 - Error trying to serialize a list of objects in my interface 具有不同字段的对象之间的通用接口 - Common interface between objects with different fields Unity IoC解析并注入一组实现通用接口的类 - Unity IoC resolve and inject a collection of classes implementing a common interface 遍历实现公共接口的对象列表 - Iterating over a list of objects that implement a common interface 在接口中实现接口属性? - Implementing interface properties in interfaces?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM