I am trying to access a json element with the name "long", but VS gives an error, it detects it as a 16 bit signed integer. The other elements in Json I can access , except element "long" and "short". Is there a way around this?
var resultOpenPositions = JsonConvert.DeserializeObject<Root>(JsonopenPositions);
string ShrtLong = resultOpenPositions.positions[0].long.units; // long here gives error , vs detects it as a 16 bit signed integer
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Long
{
public string averagePrice { get; set; }
public string pl { get; set; }
public string resettablePL { get; set; }
public List<string> tradeIDs { get; set; }
public string units { get; set; }
public string unrealizedPL { get; set; }
}
public class Short
{
public string pl { get; set; }
public string resettablePL { get; set; }
public string units { get; set; }
public string unrealizedPL { get; set; }
public string averagePrice { get; set; }
public List<string> tradeIDs { get; set; }
}
public class Position
{
public string instrument { get; set; }
public Long @long { get; set; }
public string pl { get; set; }
public string resettablePL { get; set; }
public Short @short { get; set; }
public string unrealizedPL { get; set; }
}
public class Root
{
public string lastTransactionID { get; set; }
public List<Position> positions { get; set; }
}
here is the json part:
{
"positions": [
{
"instrument": "EUR_USD",
"long": {
"units": "1",
"averagePrice": "1.13093",
"pl": "-1077.2255",
"resettablePL": "-1077.2255",
"financing": "-48.6223",
"dividendAdjustment": "0.0000",
"guaranteedExecutionFees": "0.0000",
"tradeIDs": [
"17800"
],
"unrealizedPL": "-0.0001"
},
"short": {
"units": "0",
"pl": "-543.3196",
"resettablePL": "-543.3196",
"financing": "-3.1941",
"dividendAdjustment": "0.0000",
"guaranteedExecutionFees": "0.0000",
"unrealizedPL": "0.0000"
},
"pl": "-1620.5451",
"resettablePL": "-1620.5451",
"financing": "-51.8164",
"commission": "0.0000",
"dividendAdjustment": "0.0000",
"guaranteedExecutionFees": "0.0000",
"unrealizedPL": "-0.0001",
"marginUsed": "0.0333"
}
],
"lastTransactionID": "17800"
}
CodePudding user response:
Prefix the call to the property name with an @
:
Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier, but if is not because if is a keyword.
string ShrtLong = resultOpenPositions.positions[0][email protected];
Alternativelly, configure the serializer to handle camel cased json
elements while having Pascal
cased properties on your model.
CodePudding user response:
IMHO, the best way is to use [JsonProperty] attribute. And you don't need 2 classes Long and Short, it would be enough just one.
Code
var resultOpenPositions = JsonConvert.DeserializeObject<Root(JsonopenPositions);
string ShrtLong = resultOpenPositions.positions[0].longPosition.units;
classes
public class Root
{
public string lastTransactionID { get; set; }
public List<Position> positions { get; set; }
}
public class Position
{
public string pl { get; set; }
public string instrument { get; set; }
[JsonProperty("long")]
public PositionPL longPosition { get; set; }
[JsonProperty("short")]
public PositionPL shortPosition { get; set; }
public string resettablePL { get; set; }
public string unrealizedPL { get; set; }
}
public class PositionPL
{
public string pl { get; set; }
public string averagePrice { get; set; }
public string resettablePL { get; set; }
public List<string> tradeIDs { get; set; }
public string units { get; set; }
public string unrealizedPL { get; set; }
}