【问题标题】:Ignore json property if another property returns false?如果另一个属性返回 false,则忽略 json 属性?
【发布时间】:2022-01-04 13:13:25
【问题描述】:

正如标题所暗示的,我有一个多层 json 文件,其中包含很多属性。如果 JsonProperty"use-stepover" 为 false,我需要省略 jsonProperty "stepdown"。

我尝试了 json ignore nullvaluehandling,但该值为 0.0 而不是 null 值,因此无法正常工作。 想知道我是否应该使用 Json 默认值并将其设置为 null

这里有一些代码供那些希望看到它的人使用。最后两个道具就是我在说的。

JSON 类

public partial class Preset
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("f_n")]
    public double? FN { get; set; }

    [JsonProperty("f_z")]
    public double? FZ { get; set; }

    [JsonProperty("guid")]
    public Guid Guid { get; set; }

    [JsonProperty("n")]
    public double N { get; set; }

    [JsonProperty("n_ramp")]
    public double? NRamp { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("tool-coolant")]
    public string ToolCoolant { get; set; }

    [JsonProperty("use-stepdown")]
    public bool UseStepdown { get; set; }

    [JsonProperty("use-stepover")]
    public bool UseStepover { get; set; }

    [JsonProperty("v_c")]
    public double VC { get; set; }

    [JsonProperty("v_f")]
    public double? VF { get; set; }

    [JsonProperty("v_f_leadIn")]
    public double? VFLeadIn { get; set; }

    [JsonProperty("v_f_leadOut")]
    public double VFLeadOut { get; set; }

    [JsonProperty("v_f_plunge")]
    public double VFPlunge { get; set; }

    [JsonProperty("v_f_ramp")]
    public double VFRamp { get; set; }

    [JsonProperty("v_f_retract")]
    public double VFRetract { get; set; }

    [JsonProperty("stepdown", NullValueHandling = NullValueHandling.Ignore)]
    public double Stepdown { get; set; }

    [JsonProperty("stepover", NullValueHandling = NullValueHandling.Ignore)]
    public double Stepover { get; set; }
}

【问题讨论】:

    标签: c# json


    【解决方案1】:

    快速浏览一下stackoverflow,这是一个超级简单的答案。

    将双倍改为双倍?允许值为空。

     [JsonProperty("stepdown", NullValueHandling = NullValueHandling.Ignore)]
    public double Stepdown { get; set; }
    
    [JsonProperty("stepover", NullValueHandling = NullValueHandling.Ignore)]
    public double Stepover { get; set; }
    

     [JsonProperty("stepdown", NullValueHandling = NullValueHandling.Ignore)]
    public double? Stepdown { get; set; }
    
    [JsonProperty("stepover", NullValueHandling = NullValueHandling.Ignore)]
    public double? Stepover { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      相关资源
      最近更新 更多