5

The response to my web request coming as the following (not under my control):

{
"nasdaq_imbalance": 
{
    "name": "nasdaq_imbalance", 
    "group": "Market Data", 
    "description": null
},
"DXOpen IM": 
{
    "name": "DXOpen IM", 
    "group": "Daily",
    "description": null
}, 
"Float Shares": 
{
    "name": "Float Shares", 
    "group": "Daily", 
    "description": null
}, 

}

Somehow, I need to deserialize that into C# object that contains a list of objects... Basically I need a list of objects like that:

public class Dataset    {
    public string name { get; set; } 
    public string group { get; set; } 
    public string description { get; set; } 
}
Charlieface
  • 52,284
  • 6
  • 19
  • 43
Leon
  • 165
  • 12
  • You can go to http://quicktype.io and paste your json in there, it will generate the classes, and the deser code, and even comments to tell you how to use it – Caius Jard Jan 14 '21 at 22:04
  • @CaiusJard that probably isn't the best duplicate to use, considering JavaScriptSerializer is deprecated. – David L Jan 14 '21 at 22:13
  • That's the trouble with C# JSON Qs.. too many duplicates.. https://stackoverflow.com/questions/25052293/deserialize-json-to-c-sharp-classes – Caius Jard Jan 14 '21 at 22:20
  • @CaiusJard The issue is with SO. It's easier for me to type a short answer like that than to try find a duplicate and hope a moderator actually looks at it when I flag it. – Charlieface Jan 14 '21 at 22:27
  • A better duplicate is [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/q/24536533/3744182). The answers there say to 1) Use a dictionary if the root object property names are not fixed; 2) If the root object property names are fixed, use a root object with properties marked with `JsonPropertyAttribute` attributes, e.g. `[JsonProperty("DXOpen IM")] public Dataset DXOpenIM { get; set; }`. – dbc Jan 15 '21 at 01:36

1 Answers1

9

If you are using Json.NET, you can use JsonConvert.DeserializeObject<Dictionary<string, Dataset>>(json) and the keys of the dictionary will be nasdaq_imbalance, DXOpen IM, Float Shares

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • I will try shortly, so far it is the most promising solution posted here...BTW, I was searching here A to Z and could not find similar structure since it is kinda pseudo-jason – Leon Jan 14 '21 at 22:22
  • Great thinking Charlieface! From the Dictionary I can easily make the list... – Leon Jan 14 '21 at 22:26