Home > OS >  Deserialize JSON items
Deserialize JSON items

Time:08-06

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?

CodePudding user response:

Your JSON needs to have the same spelling and capitalization as your class. For example your account number field is "account-number" in your JSON but your class spells it as "AccountNumber". Either change the JSON or change your class so that the field names match, though it would be neater to change the JSON.

CodePudding user response:

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

   
    }
  • Related