[英]how to set default value to null for datetime variable in Json
i use a json and the class definition is described below 我使用json,并且类别定义如下所述
how do i set a default value of null for 我如何为设置默认值null
[JsonProperty("Time2ATR")] public DateTime time2atr { get; set; }
so i dont get a default value of "0001-01-01T00:00:00" if i dont assign any value to it? 所以如果我不给它分配任何值,我不会得到默认值“ 0001-01-01T00:00:00”?
full code and datasample below 完整的代码和下面的数据样本
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
}');
If you want null you have to use Nullable DateTime DateTime?
如果要为null,则必须使用Nullable DateTime DateTime?
, DateTime is struct and it's default value is "0001-01-01T00:00:00" so it won't ever be null. ,DateTime是struct,其默认值为“ 0001-01-01T00:00:00”,因此永远不会为null。
Check this answer from another question Using DefaultValueHandling 使用DefaultValueHandling检查另一个问题的答案
I havent check it myself but you can give it a try 我还没有亲自检查,但您可以尝试一下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.