我有一个函数返回JSON,如下所示:

jsonParsed = {
  "account-number": "xxxxxxxx",
  "symbol": "XLE   230120C00070000",
  "instrument-type": "Equity Option",
  "underlying-symbol": "XLE",
  "quantity": 3,
  "quantity-direction": "Short",
  "close-price": "7.64",
  "average-open-price": "7.95",
  "average-yearly-market-close-price": "7.95",
  "average-daily-market-close-price": "7.64",
  "multiplier": 100,
  "cost-effect": "Debit",
  "is-suppressed": false,
  "is-frozen": false,
  "restricted-quantity": 0,
  "expires-at": "2023-01-20T15:15:00-06:00",
  "realized-day-gain": "0.0",
  "realized-day-gain-effect": "None",
  "realized-day-gain-date": "2022-07-05",
  "realized-today": "0.0",
  "realized-today-effect": "None",
  "realized-today-date": "2022-07-05",
  "created-at": "xxxx-xx-xxxxx:xx:xx.xxx-xx:00",
  "updated-at": "xxxx-xx-xxxxx:xx:xx.xx-xx:00"
}

我正在try 将其反序列化为C#类:

public class PositionResult
{
    public string AccountNumber { get; set; }
    public string Symbol { get; set; }
    public string InstrumentType { get; set; }
    public string UnderlyingSymbol { get; set; }
    public string Quantity { get; set; }
    public string QuantityDirection { get; set; }
    public string ClosePrice { get; set; }
    public string AverageOpenPrice { get; set; }
    public string AverageYearlyMarketClosePrice { get; set; }
    public string AverageDailyMarketClosePrice { get; set; }
    public string multiplier { get; set; }
    public string CostEffect { get; set; }
    public string IsSuppressed { get; set; }
    public string IsFrozen { get; set; }
    public string RestrictedQuantity { get; set; }
    public string ExpiresAt { get; set; }
    public string RealizedDayGain { get; set; }
    public string RealizedDayGainEffect { get; set; }
    public string RealizedDayGainDate { get; set; }
    public string RealizedToday { get; set; }
    public string RealizedTodayEffect { get; set; }
    public string RealizedTodayDate { get; set; }
    public string CreatedAt { get; set;}
    public string UpdatedAt { get; set;}

    public override string ToString()
    {
        return this.ReportAllProperties();
    }
}

dataDeSerialized = JsonConvert.DeserializeObject<PositionResult>(jsonParsed, new 
JsonSerializerSettings()
{
     NullValueHandling = NullValueHandling.Ignore
});

然而,除了少数例外,dataDeSerialized中的大多数字段都是空的.我怀疑"xxxx" : "xxxx"格式有问题,但我不确定我做错了什么?

推荐答案

C#类属性名称应与json属性名称相同.同步它们的常见方法是对Newtonsoft.Json使用[JsonProperty]属性.试试这门课

    public class PositionResult
   {
        [JsonProperty("account-number")]
        public string AccountNumber { get; set; }

        [JsonProperty("symbol")]
        public string Symbol { get; set; }

        [JsonProperty("instrument-type")]
        public string InstrumentType { get; set; }

        [JsonProperty("underlying-symbol")]
        public string UnderlyingSymbol { get; set; }

        [JsonProperty("quantity")]
        public long Quantity { get; set; }

        [JsonProperty("quantity-direction")]
        public string QuantityDirection { get; set; }

        [JsonProperty("close-price")]
        public string ClosePrice { get; set; }

        [JsonProperty("average-open-price")]
        public string AverageOpenPrice { get; set; }

        [JsonProperty("average-yearly-market-close-price")]
        public string AverageYearlyMarketClosePrice { get; set; }

        [JsonProperty("average-daily-market-close-price")]
        public string AverageDailyMarketClosePrice { get; set; }

        [JsonProperty("multiplier")]
        public long Multiplier { get; set; }

        [JsonProperty("cost-effect")]
        public string CostEffect { get; set; }

        [JsonProperty("is-suppressed")]
        public bool IsSuppressed { get; set; }

        [JsonProperty("is-frozen")]
        public bool IsFrozen { get; set; }

        [JsonProperty("restricted-quantity")]
        public long RestrictedQuantity { get; set; }

        [JsonProperty("expires-at")]
        public DateTimeOffset ExpiresAt { get; set; }

        [JsonProperty("realized-day-gain")]
        public string RealizedDayGain { get; set; }

        [JsonProperty("realized-day-gain-effect")]
        public string RealizedDayGainEffect { get; set; }

        [JsonProperty("realized-day-gain-date")]
        public DateTimeOffset RealizedDayGainDate { get; set; }

        [JsonProperty("realized-today")]
        public string RealizedToday { get; set; }

        [JsonProperty("realized-today-effect")]
        public string RealizedTodayEffect { get; set; }

        [JsonProperty("realized-today-date")]
        public DateTimeOffset RealizedTodayDate { get; set; }

        [JsonProperty("created-at")]
        public string CreatedAt { get; set; }

        [JsonProperty("updated-at")]
        public string UpdatedAt { get; set; }

   
    }

Csharp相关问答推荐

利用.NET 8中的AddStandardResilienceDeliveries和AddStandardHedgingDeliveries实现Resiliency

为什么.Equals(SS,StringComparison. ClientCultureIgnoreCase)在Net 4.8和6.0之间不同?

. NET WireMock拒绝PostAsJsonAsync序列化

REST API端点中异步后台代码执行的类型

为什么SignalR在每个Blazor服务器应用程序启动时最多启动8个服务器?

创建临时Collection 最有效的方法是什么?堆栈分配和集合表达式之间的区别?

从c#列表中删除额外的对象&对象&>从ASP.NET WebForm返回json响应

如何在C#中使用Postman中的本地IP向本地主机上运行的本地API发出请求

在.NET核心项目中创建Startup.cs比在Program.cs中注册服务好吗?

C#中浮点数的System.Text.Json序列化问题

如何在NET 8最小API中自动记录TypedResults.Stream响应

具有可空类型的C#NOTNULL约束具有意外行为

源代码生成器:CS8795分部方法';Class1.GetS2(字符串)';必须有实现部分,因为它有可访问性修饰符?

我可以强制System.Text.Json.JsonSerializer以非递归方式工作吗?

Autofac -动态实例化:手动传递构造函数

如何在C# WinForm控件中使用Windows 10/11的黑暗主题?

如何在Xamarin.Forms中检索PanGesture事件的位置?

如何在C#中用Serilog记录类路径、方法名和行编号

如何在flutter dart中使用publicKey.xml文件进行rsa加密,我遇到了问题Exception:Could not parse BigInt

为什么使用User.IsInRole()总是返回FALSE?