[英]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 的Type
是sportCollection
的数组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.