我正在嘗試訪問名稱為“long”的 json 元素,但 VS 出現錯誤,它將其檢測為 16 位有符號整數。Json 中的其他元素我可以訪問,除了元素“long”和“short”。有沒有解決的辦法?
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; }
}
這是json部分:
{
"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"
}
uj5u.com熱心網友回復:
使用以下前綴對屬性名稱的呼叫@:
關鍵字是預定義的保留識別符號,對編譯器具有特殊含義。它們不能用作程式中的識別符號,除非它們包含 @ 作為前綴。例如,@if 是一個有效的識別符號,但 if 不是,因為 if 是一個關鍵字。
string ShrtLong = resultOpenPositions.positions[0][email protected];
或者,將序列化器配置為處理駝峰式json元素,同時Pascal 在您的模型上具有cased 屬性。
uj5u.com熱心網友回復:
恕我直言,最好的方法是使用 [JsonProperty] 屬性。而且您不需要 2 個課程 Long 和 Short,只需一個就足夠了。
代碼
var resultOpenPositions = JsonConvert.DeserializeObject<Root(JsonopenPositions);
string ShrtLong = resultOpenPositions.positions[0].longPosition.units;
班級
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; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/392600.html
下一篇:如果CancellationTokenSource延遲,則IsCancellationRequested始終為false
