I have a function that returns JSON like this:

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"
}

I am trying to deserialize this to a C# class:

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
});

However, most of the fiels in dataDeSerialized are null with a few exceptions. I suspect that the "xxxx" : "xxxx" format is the problem, but I am not sure what I am doing wrong?

推荐答案

C# class property names should be the same as json property names.Common way to syncronize them is using [JsonProperty] attribute for Newtonsoft.Json. Try this class

    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相关问答推荐

更改对象的旋转方向

CS0103 dlibdotnet和www.example.com facerect不在上下文中

如何删除文件的基础上嵌入的时间戳嵌入文件名

如何在NodaTime中为Instant添加一年?

自动映射程序在GroupBy之后使用项目

如何在没有前缀和可选后缀的情况下获取Razor Page Handler方法名称?

在此系统上已禁用获取正在运行的脚本.&在ASP.NET Core Web API中

如何捕获对ASP.NET核心应用程序的所有请求并将其发送到一个页面

try 使用C#ASP.NET收集WMI信息时访问被拒绝,但在PowerShell中工作

使用Dapper映射联接查询对象数据到使用SplitOn;

C#中Java算法的类似功能

在被Interactive Server切换后,Blazor SSR页面无法正确加载JS

我什么时候应该在Dapper中使用Connection.OpenAsync?

如何在发布NuGet包之前设置命名空间?

解决方案:延长ABP框架和ANGING OpenIddict中的令牌生命周期

EF Core 7-忽略模型绑定中的虚拟属性

在使用xUnit和Mock执行单元测试时,控制器ViewResult返回空的Model集合

如何根据分割文本的块数来计算文本的大小?

如何在C#中抽象Vector256;T<;的逻辑以支持不同的硬件配置?

自定义ConsoleForMatter中的DI/Http上下文