Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json deserialization to C# with dynamic keys [duplicate]

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; } 
}
like image 393
Leon Avatar asked Jan 14 '21 22:01

Leon


People also ask

How do I deserialize JSON data?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is JSON deserialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

How JSON deserialization works in C#?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it creates a JavaScriptSerializer instance and calls Deserialize() by passing JSON data. It returns a custom object (BlogSites) from JSON data.


1 Answers

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

like image 151
Charlieface Avatar answered Nov 02 '22 09:11

Charlieface