我有一个返回 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"
}
我正在尝试将其反序列化为 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
中的大多数字段都为空,但有一些异常(exception)。我怀疑 "xxxx": "xxxx"
格式有问题,但我不确定我做错了什么?
最佳答案
C# 类属性名称应与 json 属性名称相同。同步它们的常用方法是使用 Newtonsoft.Json 的 [JsonProperty] 属性。试试这个类(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; }
}
关于c# - 反序列化 JSON 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73255743/