1

This is the json I'm working with:

{
  "UEPS": {
    "assetType": "EQUITY",
    "assetMainType": "EQUITY",
    "cusip": "64107N206",
    "symbol": "UEPS",
    "description": "Net 1 UEPS Technologies, Inc. - Common Stock",
    "bidPrice": 4.2,
    "bidSize": 100,
    "bidId": "Q",
    "askPrice": 5.75,
    "askSize": 200,
    "askId": "K",
    "lastPrice": 4.71,
    "lastSize": 700,
    "lastId": "Q",
    "openPrice": 4.59,
    "highPrice": 4.77,
    "lowPrice": 4.59,
    "bidTick": " ",
    "closePrice": 4.71,
    "netChange": 0,
    "totalVolume": 33908,
    "quoteTimeInLong": 1643410807235,
    "tradeTimeInLong": 1643403601408,
    "mark": 4.71,
    "exchange": "q",
    "exchangeName": "NASD",
    "marginable": true,
    "shortable": true,
    "volatility": 0.3684,
    "digits": 4,
    "52WkHigh": 6.97,
    "52WkLow": 3.84,
    "nAV": 0,
    "peRatio": 0,
    "divAmount": 0,
    "divYield": 0,
    "divDate": "",
    "securityStatus": "Normal",
    "regularMarketLastPrice": 4.71,
    "regularMarketLastSize": 7,
    "regularMarketNetChange": 0,
    "regularMarketTradeTimeInLong": 1643403601408,
    "netPercentChangeInDouble": 0,
    "markChangeInDouble": 0,
    "markPercentChangeInDouble": 0,
    "regularMarketPercentChangeInDouble": 0,
    "delayed": false,
    "realtimeEntitled": true
  },
  "SRT": {
    "assetType": "EQUITY",
    "assetMainType": "EQUITY",
    "cusip": "85569C107",
    "symbol": "SRT",
    "description": "StarTek, Inc. Common Stock",
    "bidPrice": 2.77,
    "bidSize": 100,
    "bidId": "T",
    "askPrice": 5.39,
    "askSize": 0,
    "askId": " ",
    "lastPrice": 5.07,
    "lastSize": 2000,
    "lastId": "N",
    "openPrice": 5,
    "highPrice": 5.09,
    "lowPrice": 4.915,
    "bidTick": " ",
    "closePrice": 5.07,
    "netChange": 0,
    "totalVolume": 54200,
    "quoteTimeInLong": 1643418000039,
    "tradeTimeInLong": 1643414400004,
    "mark": 5.07,
    "exchange": "n",
    "exchangeName": "NYSE",
    "marginable": true,
    "shortable": true,
    "volatility": 0.2976,
    "digits": 2,
    "52WkHigh": 9.8,
    "52WkLow": 3.8,
    "nAV": 0,
    "peRatio": 0,
    "divAmount": 0,
    "divYield": 0,
    "divDate": "",
    "securityStatus": "Normal",
    "regularMarketLastPrice": 5.07,
    "regularMarketLastSize": 20,
    "regularMarketNetChange": 0,
    "regularMarketTradeTimeInLong": 1643414400004,
    "netPercentChangeInDouble": 0,
    "markChangeInDouble": 0,
    "markPercentChangeInDouble": 0,
    "regularMarketPercentChangeInDouble": 0,
    "delayed": false,
    "realtimeEntitled": true
  },
  "PTPI": {
    "assetType": "EQUITY",
    "assetMainType": "EQUITY",
    "cusip": "71678J100",
    "symbol": "PTPI",
    "description": "Petros Pharmaceuticals, Inc. - Common Stock",
    "bidPrice": 1.59,
    "bidSize": 500,
    "bidId": "P",
    "askPrice": 1.62,
    "askSize": 400,
    "askId": "P",
    "lastPrice": 1.6199,
    "lastSize": 0,
    "lastId": "D",
    "openPrice": 1.56,
    "highPrice": 1.6693,
    "lowPrice": 1.45,
    "bidTick": " ",
    "closePrice": 1.62,
    "netChange": -0.0001,
    "totalVolume": 922374,
    "quoteTimeInLong": 1643417960464,
    "tradeTimeInLong": 1643417960466,
    "mark": 1.62,
    "exchange": "q",
    "exchangeName": "NASD",
    "marginable": false,
    "shortable": false,
    "volatility": 0.2206,
    "digits": 4,
    "52WkHigh": 5.2,
    "52WkLow": 1.19,
    "nAV": 0,
    "peRatio": 0,
    "divAmount": 0,
    "divYield": 0,
    "divDate": "",
    "securityStatus": "Normal",
    "regularMarketLastPrice": 1.62,
    "regularMarketLastSize": 12,
    "regularMarketNetChange": 0,
    "regularMarketTradeTimeInLong": 1643403600790,
    "netPercentChangeInDouble": -0.0062,
    "markChangeInDouble": 0,
    "markPercentChangeInDouble": 0,
    "regularMarketPercentChangeInDouble": 0,
    "delayed": false,
    "realtimeEntitled": true
  }
}

And here is the class I am trying to deserialize

public class Qoute
    {
        public Dictionary<string, Equity> Equity { get; set; }
    }

    public class Equity
    {
        public string assetType { get; set; }
        public string assetMainType { get; set; }
        public string cusip { get; set; }
        public string symbol { get; set; }
        public string description { get; set; }
        public float bidPrice { get; set; }
        public int bidSize { get; set; }
        public string bidId { get; set; }
        public float askPrice { get; set; }
        public int askSize { get; set; }
        public string askId { get; set; }
        public float lastPrice { get; set; }
        public int lastSize { get; set; }
        public string lastId { get; set; }
        public float openPrice { get; set; }
        public float highPrice { get; set; }
        public float lowPrice { get; set; }
        public string bidTick { get; set; }
        public float closePrice { get; set; }
        public float netChange { get; set; }
        public int totalVolume { get; set; }
        public long quoteTimeInLong { get; set; }
        public long tradeTimeInLong { get; set; }
        public float mark { get; set; }
        public string exchange { get; set; }
        public string exchangeName { get; set; }
        public bool marginable { get; set; }
        public bool shortable { get; set; }
        public float volatility { get; set; }
        public int digits { get; set; }
        public float _52WkHigh { get; set; }
        public float _52WkLow { get; set; }
        public float nAV { get; set; }
        public float peRatio { get; set; }
        public float divAmount { get; set; }
        public float divYield { get; set; }
        public string divDate { get; set; }
        public string securityStatus { get; set; }
        public float regularMarketLastPrice { get; set; }
        public int regularMarketLastSize { get; set; }
        public float regularMarketNetChange { get; set; }
        public long regularMarketTradeTimeInLong { get; set; }
        public float netPercentChangeInDouble { get; set; }
        public float markChangeInDouble { get; set; }
        public float markPercentChangeInDouble { get; set; }
        public float regularMarketPercentChangeInDouble { get; set; }
        public bool delayed { get; set; }
        public bool realtimeEntitled { get; set; }
    }




}

When I try to convert to a List<> of my class I get this response: var x= JsonConvert.DeserializeObject<List<Model.Qoutes.Qoute>>(response); Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List1[TDAPI.Model.Qoutes.Qoute]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal`

When I try to just get a single object, the Class Equity is null: var x2 = JsonConvert.DeserializeObject<Model.Qoutes.Qoute>(response);

My understanding is that public Dictionary<string, Equity> Equity { get; set; } should have taken care of a dynamic property name. Am I missing something obvious?

  • Does this answer your question? [json deserialization to C# with dynamic keys](https://stackoverflow.com/questions/65727513/json-deserialization-to-c-sharp-with-dynamic-keys) – Charlieface Jan 30 '22 at 01:16

1 Answers1

0

Provided json has an object as a root, not an array, and this object does not have Equity property, as your Quote model does, it has properties containing objects you want to map to Equity so try:

var result = JsonConvert.DeserializeObject<Dictionary<string, Equity>>(response);
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Oh - I see. My issue was I was serializing the wrong class. I was assuming since the class Qoute had public Dictionary Equity { get; set; } it would deserialize into a list at that level – user1257758 Jan 29 '22 at 20:50
  • In fact, I should have named my Equity class Qoute if that was my intention – user1257758 Jan 29 '22 at 20:51