1

我正在尝试访问名为“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"
}
4

2 回答 2

1

恕我直言,最好的方法是使用 [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; }
    }
于 2021-12-25T00:42:38.487 回答
1

使用以下前缀对属性名称的调用@

关键字是预定义的、保留的标识符,对编译器具有特殊含义。除非它们包含 @ 作为前缀,否则它们不能用作程序中的标识符。例如,@if 是有效标识符,但 if 不是因为 if 是关键字。

string ShrtLong = resultOpenPositions.positions[0].@long.units;

或者,将序列化程序配置为处理驼峰式json元素,同时Pascal 在模型上具有大小写属性。

于 2021-12-24T19:16:43.860 回答