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

我想从使用 ASP.NET Core 6 的 API 收到的 XML 字符串创建 docx 文件

反序列化 JSON 项

如何使用 Language-Ext 将 Task> 绑定到 Either<>

如何从正则表达式中获取替换字符串列表

CSV 在标头中的双引号上似乎已损坏 - C#

3SUM - O(n^2 * log n) 比 O(n^2) 慢?

Windows 服务中托管的 .NET 6 BackgroundServices 中的错误处理

如何使用 Imagemagick.Net 判断模糊图像

DateTime.TryParse() 在具有显式文化的不同计算机上返回不同的结果

在 OpenTK 上绑定多个纹理的正确方法是什么?

使用“with”表达式将“记录”(C#)复制到不同的“记录”时,如何附加值?

更好地理解 Entity Framework Core 查询和 DbSet 影响

将 c++ std::string 转换为 c# 字节数组

在 C# 中引发 EventArg 具有通用属性的事件

为什么 C# IndexOf 搜索空字符返回零?

Newtonsoft.Json.JsonSerializationException:从“Castle.Proxies.SalonProxy”上的“MyEntity”获取值时出错

如何在确定性 ECDSA - Bouncy Castle 上应用 P1363 格式?

如何创建可以使用任何 Ef Core DbContext 的类库

使用逆变 IEqualityComparer 调用泛型函数

webbrowser 中的 Selenium webdriver