从响应中删除 属性 名称

Remove property name from the response

public class CarsModel
{        
    public int OnStock { get; set; }
    public int Rented { get; set; }
    public List<CarInfo> Cars { get; set; }
}
    
public class CarInfo
{
   [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public CarDetails Details { get; set; }

   [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
   public ServiceInfo ServiceInfo { get; set; }

   [JsonProperty( NullValueHandling = NullValueHandling.Ignore)]
   public MarketingInfo MarketingInfoDetails { get; set; }
 }  

根据场景的不同,这辆汽车 属性 将有不同的表示(MarketingInfo 或 CarDetails 或 ServiceInfo,但不能两者都有)。 案例 1:营销信息

"onStock": 11,
"rented": 2,
"cars": [{
          "carId": 1,
          "MarketingInfo": {           
            "contactPerson": "John Smith"
          }, ...
        }]
    

案例 2:ServiceInfo 和 CarDetails

"onStock": 11,
"rented": 2,
"cars": [{       
          "ServiceInfo": {           
            "lastService": "2021-01-01"         
          }, 
          "CarDetails": {           
            "carId": 1,
            "lastOwner": "Mister Clark"
          }, ...
        }]

这就是我填充回复的方式

var carsModel = new CarsModel{
    OnStock = 11,
    Rented = 2
};

foreach (var car in cars)
{
    carsModel.Cars.Add(new CarsModel 
    {
        MarketingInfoDetails = new MarketingInfo
        {
            ContactPerson = "John Smith",
            ....
        }
    });
}

在上面的模型表示中,我的回复不是我想要的(因为它包含 marketingInfoDetails 词)

Response:
{
  "data" :[{
      "onStock": 11,
      "rented": 2,
          "cars": [
            {
              "marketingInfoDetails": {
                "contactPerson": "John Smith",
                ...
              }
            }]
    }]
}

我希望在没有名称“marketingInfoDetails”的情况下被代表

{
    "data" : [{ 
        "onStock" : 11, 
        "rented" : 2, 
        "cars" : [{ "contactPerson" : "John Smith"}] 
    }] 
}

如果您认为可以更好地为描述的场景建模,请分享。

您的 C# 模型和结果 JSON 匹配。从问题的开始到问题的结尾,你要求的变化是什么。您似乎希望从 C# 模型和 JSON 中删除您想要的示例包含的对象以及您的 C# 模型中不存在的 carId 属性。

您需要删除中间 CarInfo class 并让您的汽车列表成为由 CarDetailsServiceDetailsMarketingInfo,那么您将不会在 JSON.

中看到“MarketingInfoDetails”属性

如果您改为将 carId 属性 添加到 CarInfo 并再次将 C# 模型序列化为 JSON,您将看到您当前的模型完全匹配你第一次说你想要 JSON 看起来像什么。

如果您使用 Json.net,您可以使用 ShouldSerialize

来使用 Conditional Serialization functionality

To conditionally serialize a property, add a method that returns boolean with the same name as the property and then prefix the method name with ShouldSerialize. The result of the method determines whether the property is serialized. If the method returns true then the property will be serialized, if it returns false then the property will be skipped.

您需要修改您的 DTO 以包含额外的 属性 和方法(对于每个 属性 您想要设置条件),如下所示。

[JsonIgnore]
public bool SerializeDetails{get;set;} = true;
    
public bool ShouldSerializeDetails() =>SerializeDetails;

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public CarDetails Details { get; set; }


[JsonIgnore]
public bool SerializeMarketingInfoDetails{get;set;} = true;
public bool ShouldSerializeMarketingInfoDetails() =>SerializeMarketingInfoDetails;
    
[JsonProperty( NullValueHandling = NullValueHandling.Ignore)]
public MarketingInfo MarketingInfoDetails { get; set; }

对于 CarDetails 的每个实例,您现在可以设置 SerializeMarketingInfoDetailsSerializeDetails 属性以有条件地序列化它们。

Demo Code

建议您对数据成员使用 EmiDefaultValue="true"。 在proprety为null的情况下,它不会显示在json中,因为它的值为null。