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; } 
}
5
  • You can go to 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 Commented Jan 14, 2021 at 22:04
  • @CaiusJard that probably isn't the best duplicate to use, considering JavaScriptSerializer is deprecated. Commented Jan 14, 2021 at 22:13
  • That's the trouble with C# JSON Qs.. too many duplicates.. stackoverflow.com/questions/25052293/… Commented Jan 14, 2021 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. Commented Jan 14, 2021 at 22:27
  • A better duplicate is How can I parse a JSON string that would cause illegal C# identifiers?. 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; }. Commented Jan 15, 2021 at 1:36

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
Great thinking Charlieface! From the Dictionary I can easily make the list...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.