如何将Json中的datetime变量的默认值设置为null

本文关键字:默认值 设置 null 变量 datetime Json 中的 | 更新日期: 2023-09-27 18:03:25

我使用json,类定义如下

如何设置

的默认值为null
[JsonProperty("Time2ATR")]
   public DateTime time2atr { get; set; }

所以我没有得到一个默认值"0001-01-01T00:00:00",如果我不分配任何值给它?

完整代码和数据示例如下

 using Newtonsoft.Json;
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.STSClasses
{
    /// <summary>
    /// This file holds all user defined indicator methods.
    /// </summary>
   public class STSmyTrade
    {
        [JsonProperty("direction")]
        public string direction { get; set; }
        [JsonProperty("entryprice")]
        public double entry { get; set; }
        [JsonProperty("exitprice")]
        public double exit { get; set; }
        [JsonProperty("potentialtarget")]
        public double potentialtarget { get; set; }
        [JsonProperty("entrytime")]
        public DateTime entrytime { get; set; }
        [JsonProperty("exittime")]
        public DateTime exittime { get; set; }
        [JsonProperty("maxfavourable")]
        public double mfe { get; set; }
        [JsonProperty("maxagainst")]
        public double mae { get; set; }

        [JsonProperty("maxagainst1ATR")]
        public double mae1atr { get; set; }
        [JsonProperty("Time1ATR")]
        public DateTime time1atr { get; set; }
        [JsonProperty("maxagainst2ATR")]
        public double mae2atr { get; set; }
        [JsonProperty("Time2ATR")]
       public DateTime time2atr { get; set; }
        [JsonProperty("gains")]
        public double gains { get; set; }
        [JsonProperty("signal")]
        public string signal { get; set; }
        [JsonProperty("instrument")]
        public string instrument { get; set; }
        [JsonProperty("account")]
        public string account { get; set; }
         [JsonProperty("quantity")]
        public int quantity { get; set; }
         [JsonProperty("hitedge")]
        public bool hitedge { get; set; }
         [JsonProperty("RealizedProfitLoss")]
        public double RealizedProfitLoss { get; set; }
        [JsonProperty("CashValue")]
        public double CashValue { get; set; }
        [JsonProperty("BuyingPower")]
        public double BuyingPower { get; set; }
    }
}
    {
      "direction": "Long",
      "entryprice": 1.13211,
      "exitprice": 1.13197,
      "potentialtarget": 0.00025,
      "entrytime": "2016-08-22T13:46:31.7459655-04:00",
      "exittime": "2016-08-22T15:46:22.3955682-04:00",
      "maxfavourable": 0.00055,
      "maxagainst": -0.00035,
      "maxagainst1ATR": -0.00035,
      "Time1ATR": "0001-01-01T00:00:00",
      "maxagainst2ATR": -0.00035,
      "Time2ATR": "0001-01-01T00:00:00",
      "gains": -0.00015,
      "signal": "EnterLongBounce",
      "instrument": "$EURUSD",
      "account": "InteractiveBrokersindicatorbased",
      "quantity": 1,
      "hitedge": false,
      "RealizedProfitLoss": 0.0,
      "CashValue": 0.0,
      "BuyingPower": 0.0
    }');

如何将Json中的datetime变量的默认值设置为null

如果你想要null,你必须使用Nullable DateTime DateTime?, DateTime是struct,它的默认值是"0001-01-01T00:00:00"所以它永远不会是null

使用DefaultValueHandling检查另一个问题的答案

我自己没有检查过,但是你可以试试