I am trying to get the value of "accountBalance", but beside of that I get also a bunch of unwanted "0". It seems that I get the "0" on every element that has no "accountBalance" key . How could I select only those elements that contains the key "accountBalance".
My Code:
var resultOpTrades = JsonConvert.DeserializeObject<RootObject>(result);//Deserialize Json
var pricelist = resultOpTrades.transactions.Select(p => p.accountBalance).ToList().Select(s => Convert.ToDouble(s)).ToList();
pricelist.ForEach(Console.WriteLine);
JSON:
{"transactions": [
{
"type": "CLIENT_CONFIGURE",
"marginRate": "0.02",
"alias": "Test USD",
"id": "2",
"userID": 4455670,
"accountID": "101-004-4455670-004",
"batchID": "1",
"requestID": "1789786643428780285",
"time": "2018-01-22T13:01:57.930423995Z"
},
{
"accountBalance": "2000.0000",
"type": "TRANSFER_FUNDS",
"amount": "2000.0000000000",
"fundingReason": "ADJUSTMENT",
"id": "3",
"userID": 4455670,
"accountID": "101-004-4455670-004",
"batchID": "3",
"requestID": "1735743448013647784",
"time": "2018-01-22T13:02:24.580177329Z"
},
{
"type": "MARKET_ORDER",
"instrument": "EUR_JPY",
"units": "-2000",
"timeInForce": "FOK",
"positionFill": "DEFAULT",
"reason": "CLIENT_ORDER",
"id": "4",
"userID": 4455670,
"accountID": "101-004-4455670-004",
"batchID": "4",
"requestID": "60404387589188847",
"time": "2018-01-22T13:06:12.138121604Z"
},
{
"type": "ORDER_FILL",
"orderID": "4",
"instrument": "EUR_JPY",
"units": "-2000",
"price": "135.627",
"pl": "0.0000",
"financing": "0.0000",
"commission": "0.0000",
"accountBalance": "2000.0000",
"gainQuoteHomeConversionFactor": "0.009022013713",
"lossQuoteHomeConversionFactor": "0.009023071995",
"guaranteedExecutionFee": "0.0000",
"halfSpreadCost": "0.1353",
"fullVWAP": "135.627",
"reason": "MARKET_ORDER",
"tradeOpened": {
"price": "135.627",
"tradeID": "5",
"units": "-2000",
"guaranteedExecutionFee": "0.0000",
"halfSpreadCost": "0.1353"
},
"fullPrice": {
"closeoutBid": "135.627",
"closeoutAsk": "135.642",
"timestamp": "2018-01-22T13:05:56.780436649Z",
"bids": [
{
"price": "135.627",
"liquidity": "10000000"
}
],
"asks": [
{
"price": "135.642",
"liquidity": "10000000"
}
]
},
"id": "5",
"userID": 4455670,
"accountID": "101-004-4455670-004",
"batchID": "4",
"requestID": "60404387589188847",
"time": "2018-01-22T13:06:12.138121604Z"
},
{
"type": "MARKET_ORDER",
"instrument": "EUR_JPY",
"units": "2000",
"timeInForce": "FOK",
"positionFill": "REDUCE_ONLY",
"reason": "TRADE_CLOSE",
"tradeClose": {
"units": "ALL",
"tradeID": "5"
},
"id": "6",
"userID": 4455670,
"accountID": "101-004-4455670-004",
"batchID": "6",
"requestID": "60404387832520278",
"time": "2018-01-22T13:07:10.544407912Z"
},],"lastTransactionID": "22083"}
The result I get: 0, 2000, 0, 2000, 0,
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]. That's why some properties are missing in your JSON item. So you can either check if it is null or zero before using it.